22 Dec Series in Pandas
Series in Pandas is a one-dimensional array, like a column in a table. It is a labeled array that can hold data of any type. The Series() method is used for this and has the following parameters:
- data: The data to be stored in the Pandas Series
- index: The index values should have the same length as the data.
- dtype: It is the datatype for the output Series.
- name: Set the series name with the name parameter
- copy: To copy the input data
In this lesson, we will understand what is Series with the following examples:
- Create a Pandas Series
- Access a value from a Pandas Series
- Name your own indexes in a Pandas Series
- Access a value from a Pandas Series with labels
Create a Pandas Series
To create a series in Python, we use the Series() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Data to be stored in the Pandas Series data = [10, 20, 40, 80, 100] # Create a Series using the Series() method s = pd.Series(data) # Display the Series print("Series: \n",s) |
Output
1 2 3 4 5 6 7 8 9 |
Series: 0 10 1 20 2 40 3 80 4 100 dtype: int64 |
The 0,1,2,3, etc. are the index numbers i.e. labels.
Access a value from a Pandas Series
Let us see how to access a specific value from a Series. The [] is used to access a value. Set the index of the value you want to display inside []:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import pandas as pd # Data to be stored in the Pandas Series data = [10, 20, 40, 80, 100] # Create a Series using the Series() method s = pd.Series(data) # Display the Series print("Series: \n",s) # Access a value print("\nValue from a Pandas Series: ",s[2]) |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Series: 0 10 1 20 2 40 3 80 4 100 dtype: int64 Value from a Pandas Series: 40 |
Name your own indexes in a Pandas Series
The index argument is used to set and name your own indexes in a Series i.e. the labels can be set accordingly. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Data to be stored in the Pandas Series data = [10, 20, 40, 80, 100] # Create a Series using the Series() method s = pd.Series(data, index = ["RowA", "RowB", "RowC", "RowD", "RowE"]) # Display the Series print("Series (with custom index labels): \n",s) |
Output
1 2 3 4 5 6 7 8 9 |
Series (with custom index labels): RowA 10 RowB 20 RowC 40 RowD 80 RowE 100 dtype: int64 |
Access a value from a Pandas Series with labels
If you have set the custom index for labels as shown above, then accessing any value from the Series is quite easy. Refer to the label and that’s it. Let’s see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import pandas as pd # Data to be stored in the Pandas Series data = [10, 20, 40, 80, 100] # Create a Series using the Series() method s = pd.Series(data, index = ["RowA", "RowB", "RowC", "RowD", "RowE"]) # Display the Series print("Series (with custom index labels): \n",s) # Access a value referring the lable print("\nValue from a Pandas Series with label RowD: ",s["RowD"]) |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Series (with custom index labels): RowA 10 RowB 20 RowC 40 RowD 80 RowE 100 dtype: int64 Value from a Pandas Series with label RowD: 80 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments