Series¶
Let’s say we have a cat and we noticed it is sneezing a lot. We suspect it might be allergic to something. So we track the count of sneezes over one week. For this purpose, we could employ the Series data type provided by pandas.
Start by importing it:
Creating a Series¶
There are different ways we can add data to a Series. We start out with a simple list:
Note that the Series automatically adds an index on the left side.
It also automatically infers the best fitting data type for the elements (here int64
= 64-bit integer)
Note: If you are not familiar with Object-oriented Programming you might be caught a bit off guard by the way this actually works. In short, pandas introduces the series as a new data type (like
int
,str
and all the others) and as such the value ofsneeze_counts
is actually the whole series at once.
Extra Information¶
To make the data a bit more meaningful, let’s set a custom index:
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
sneeze_counts.index = days_of_week
print(sneeze_counts)
Also, we add a name to the series, so we can distinguish it later:
All at Once¶
The index and name can also be passed directly while creating the series
We suspect that the illness of our cat is related to the weather, so we also log the average temperature and humidity
temperatures = Series(
data=[10.9, 8.2, 7.6, 7.8, 9.4, 11.1, 12.4],
index=days_of_week,
name="Temperature"
)
humidities = Series(
data=[62.5, 76.3, 82.4, 98.2, 77.4, 58.9, 41.2],
index= days_of_week,
name="Humidity"
)
Alternatively you can provide the index while creating the series by passing a dictionary:
Quick Maths¶
To get a first statistical impression of the data, use the describe()
-method:
Output
Key Points
- Series are a 1-dimensional data structure
- You can use indices to label the data and a name to label the whole Series