I wanted to reassure you that a
pd.Series
is like a Python
list,
but I ended up demonstrating that a
pd.Series
is like a Python
dict.
"Iterate through a pd.Series."
import sys
import pandas as pd
data = [0.0, 10.0, 20.0, 30.0, 40.0]
series = pd.Series(data = data, name = "temperature")
print(series)
print()
print(f"{len(series) = }")
print(f"{3 in series = }") #Is 3 in the index of the series?
print(f"{5 in series = }") #like using in with a Python dict
print(f"{30.0 in series.array = }") #Is 30.0 in the values of the series?
print(f"{50.0 in series.array = }") #see also Series.isin
print()
#Print each float in the values column.
for f in series:
print(f)
print()
#Print each integer in the index column.
for i in series.index:
print(i)
print()
#Print both, in parallel.
for i, f in series.items(): #two variables, like looping through a dict using items
print(i, f)
sys.exit(0)
0 0.0 1 10.0 2 20.0 3 30.0 4 40.0 Name: temperature, dtype: float64 len(series) = 5 3 in series = True 5 in series = False 30.0 in series.array = True 50.0 in series.array = False 0.0 10.0 20.0 30.0 40.0 0 1 2 3 4 0 0.0 1 10.0 2 20.0 3 30.0 4 40.0
Series.
series[:3]
is a
slice
of the
Series.
for i, f in series[:3].items(): #also try series[-3:] print(i, f)
0 0.0 1 10.0 2 20.0
Series.
for i, f in series[::2].items(): print(i, f)
0 0.0 2 20.0 4 40.0
reversed
function.
for i, f in series[::-1].items(): print(i, f)
4 40.0 3 30.0 2 20.0 1 10.0 0 0.0