Easy Tutorial
❮ Pandas Csv File Pandas Json ❯

Pandas Data Structure - Series

A Pandas Series is similar to a column in a table, akin to a one-dimensional array, capable of holding any data type.

A Series consists of an index and columns, with the function as follows:

pandas.Series(data, index, dtype, name, copy)

Parameter descriptions:

Creating a simple Series instance:

Example

import pandas as pd

a = [1, 2, 3]

myvar = pd.Series(a)

print(myvar)

Output is as follows:

From the figure, if no index is specified, the index values start from 0. We can retrieve data based on the index value:

Example

import pandas as pd

a = [1, 2, 3]

myvar = pd.Series(a)

print(myvar[1])

Output is as follows:

2

We can specify index values, as shown in the following example:

Example

import pandas as pd

a = ["Google", "tutorialpro", "Wiki"]

myvar = pd.Series(a, index=["x", "y", "z"])

print(myvar)

Output is as follows:

Retrieving data by index value:

Example

import pandas as pd

a = ["Google", "tutorialpro", "Wiki"]

myvar = pd.Series(a, index=["x", "y", "z"])

print(myvar["y"])

Output is as follows:

tutorialpro

We can also create a Series using a key/value object, similar to a dictionary:

Example

import pandas as pd

sites = {1: "Google", 2: "tutorialpro", 3: "Wiki"}

myvar = pd.Series(sites)

print(myvar)

Output is as follows:

From the figure, the dictionary's keys become the index values.

If we only need a portion of the dictionary's data, we can specify the required data's index, as shown in the following example:

Example

import pandas as pd

sites = {1: "Google", 2: "tutorialpro", 3: "Wiki"}

myvar = pd.Series(sites, index=[1, 2])

print(myvar)

Output is as follows:

Setting the Series name parameter:

Example

import pandas as pd

sites = {1: "Google", 2: "tutorialpro", 3: "Wiki"}

myvar = pd.Series(sites, index=[1, 2], name="tutorialpro-Series-TEST")

print(myvar)

Output is as follows:

❮ Pandas Csv File Pandas Json ❯