After moving ahead from installation and importing pandas, we now come to understand a very basic concept of pandas, which is “Series” in Pandas. Till now, we have only gained some theoretical knowledge about Pandas, but now it’s time to go practical and understand concepts in Pandas.
So, the very first question that comes to our mind – what is a series?
What is Series in Pandas?
We can understand a Series as a column in the table. You can understand the Series as a one-dimensional labeled array, which can hold any type of data(integer, string, float, etc). You will understand more about this when you would see the Series in action. Now I want to say that it’s time for you to implement rather than just read.
import pandas as PD
arr = [1, 2, 3, 4, 5, 6, 7, 8]
num_series = pd.Series(arr)
print(num_series)
Output –
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
dtype: int64
As you can see, we could get the series as output, with the data from 1 to 8. The 0, 1, 2, 3 … that you can see, are labels. When we have specified nothing else, these are labeled as index numbers, as it can be seen that the first is 0, the second is 1, etc. These labels can be used to access the specified values. Let’s see that in the below program –
import pandas as pd
arr = [1, 2, 3, 4, 5, 6, 7, 8]
num_series = pd.Series(arr)
print(num_series[3])
output –
4
As you can see, we can access the value with label 3, which is 4 in the series. You can try it by yourself, and see how it works. If you don’t like those index numbers, or you want to create your own, you certainly can create labels! Let’s see how with the below program –
import pandas as PD
arr = [1, 2, 3, 4, 5, 6, 7, 8]
num_series = pd.Series(arr, index=["a", "b", "c", "d", "e", "f", "g", "h"])
print(num_series)
output –
a 1
b 2
c 3
d 4
e 5
f 6
g 7
h 8
dtype: int64
Although the data doesn’t make any sense, we have managed to provide our labels to the data. To do this, we had to just make use of the index attribute, while we were creating the Series. Again, we can access the specific values using the labels. Let’s try this again –
import pandas as PD
arr = [1, 2, 3, 4, 5, 6, 7, 8]
num_series = pd.Series(arr, index=["a", "b", "c", "d", "e", "f", "g", "h"])
print(num_series['g'])
Output –
7
As you can see, we were able to access the specific value in the Series. So far, we have created a series using a list, but now, let’s try to create one using a dictionary. Here is a program for the same –
import pandas as PD
placements = {"CSE": 25, "EEE": 16, "IT": 34, "Mechanical": 32}
placements_series = pd.Series(placements)
print(placements_series)
Output –
CSE 25
EEE 16
IT 34
Mechanical 32
As you can see, we could use the key-value pairs to create the dictionary, and we can see that the keys have become the label. To even specify only some items in your dictionary, you can specify only those items that you want to include in the Series. Here is an example of that –
import pandas as PD
placements = {"CSE": 25, "EEE": 16, "IT": 34, "Mechanical": 32}
placements_series = pd.Series(placements, index=["CSE", "Mechanical"])
print(placements_series)
Output –
CSE 25
Mechanical 32
So, this was just a brief introduction to Series in Pandas. You can practice creating series, but when you would be practically using pandas ahead, you might need to use DataFrame more often. So, we are going to explore about DataFrame now, but I would seriously recommend practicing Series in Pandas.