You can create Pandas Series from a list using this syntax:
pd.Series(list_name)
Steps to Create Pandas Series from a List
Step 1: Create a List
To start, let’s create a list that contains 5 names:
people_list = ['Jon', 'Mark', 'Maria', 'Jill', 'Jack'] print(people_list)
This is how the list would look like:
['Jon', 'Mark', 'Maria', 'Jill', 'Jack']
The ultimate goal is to create a Pandas Series from the above list.
Step 2: Create the Pandas Series
Next, create the Pandas Series using this template:
pd.Series(list_name)
For our example, the list_name is “people_list.” Therefore, the complete code to create the Pandas Series is:
import pandas as pd people_list = ['Jon', 'Mark', 'Maria', 'Jill', 'Jack'] my_series = pd.Series(people_list) print(my_series)
Once you run the code in Python, you’ll get the following Series:
0 Jon
1 Mark
2 Maria
3 Jill
4 Jack
Step 3 (optional): Verify that you Created the Series
You can quickly verify that you successfully created the Pandas Series by adding “print(type(my_series))” at the bottom of the code:
import pandas as pd people_list = ['Jon', 'Mark', 'Maria', 'Jill', 'Jack'] my_series = pd.Series(people_list) print(my_series) print(type(my_series))
Run the code, and you’ll be able to confirm that you got the Pandas Series:
0 Jon
1 Mark
2 Maria
3 Jill
4 Jack
dtype: object
<class 'pandas.core.series.Series'>
Change the Index of the Pandas Series
You may have noticed that each row is represented by a number (also known as the index) starting from 0:
0 Jon
1 Mark
2 Maria
3 Jill
4 Jack
Alternatively, you may assign another value/name to represent each row. For example, in the code below, the index=[‘A’, ‘B’, ‘C’, ‘D’, ‘E’] was added:
import pandas as pd people_list = ['Jon', 'Mark', 'Maria', 'Jill', 'Jack'] my_series = pd.Series(people_list, index=['A', 'B', 'C', 'D', 'E']) print(my_series)
You’ll now see the newly assigned values:
A Jon
B Mark
C Maria
D Jill
E Jack
Additional Resources
So far, you have seen how to create Pandas Series. You may also want to check the following guide to learn how to create Pandas DataFrame.
Finally, you can learn more about Pandas Series by visiting the Pandas Documentation.