What is a Series?
In Pandas, a Series is a one-dimensional labeled array that can hold data of any type — integers, strings, floats, and more.
Think of it like a single column in a spreadsheet or database table. Each value in a Series is associated with an index, which makes data selection and manipulation easy and powerful.
Program:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
Output:
Labels in a Series:
By default, each value in a Pandas Series is assigned a numeric index label, starting from 0. That means the first item has index 0, the second has 1, and so on.
These labels act like identifiers and can be used to access specific values within the Series.
Program:
Create Labels:
Program:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar)
Output:
x 1
y 7
z 2
dtype: int64
Accessing Elements Using Labels:
Once you have assigned custom labels to your data, you can retrieve specific items by referring to those labels.
Program:
Output:
Creating a Series from a Dictionary (Key/Value Objects):
In Pandas, you can create a Series using a key/value object like a Python dictionary. The keys in the dictionary automatically become the labels (index) in the Series.
Program:
Convert a Dictionary to a Series
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories)
print(myvar)
Output:
day1 420
day2 380
day3 390
dtype: int64
Selecting Specific Items Using the Index Argument:
Program:
Include Only "day1" and "day2"
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories, index=["day1", "day2"])
print(myvar)
Output:
day1 420
day2 380
dtype: int64