A Pandas Series is similar to a column in a table and is a one-dimensional array that can store data of any type.
You can create a simple Pandas Series from a list like this:
import pandas as pd a = [1, 7, 2] myvar = pd.Series(a) print(myvar) |
By default, the values are labeled with their index number, starting with 0 for the first value, 1 for the second, and so on. This index can be used to access specific values.
To retrieve the first value of the Series:
print(myvar[0]) |
The index
argument allows you to define custom labels for the values.
Define your own labels:
import pandas as pd a = [1, 7, 2] myvar = pd.Series(a, index = [“x”, “y”, “z”]) print(myvar) |
Once you have created labels, you can access an item by referencing its label.
Retrieve the value associated with the label “y”:
print(myvar[“y”]) |