Pandas Series

Dhanapriya D

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:

0    1
1    7
2    2
dtype: int64  (dtype: int64 indicates that the data type of the values is 64-bit integer.)

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:

print(myvar[0])

Output:
 
1

Create Labels:

You can assign your own labels to a Pandas Series by using the index parameter when creating the Series.

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:

Return the value associated with the label "y":

print(myvar["y"])

Output:

7

(This is because "y" is the label for the second element in the Series, which has the value 7.)

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:

If you want to include only selected items from the dictionary, you can use the index parameter to specify the desired keys.

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








Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send