How to Convert List to Pandas Series

To convert a Python list into a Pandas Series directly pass the list object as an argument to the Series() constructor. We can easily convert the list, tuple, and dictionary into a Series using the Series() function. In this article, we will explain how we can convert a Python list to a Series with several examples.

1. Quick Examples of Convert List to Series

If you are in a hurry, below are some quick examples of how to convert a python list to a series.


# Below are quick examples

# Example 1: create the Series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])

# Example 2: convert python list to pandas series
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
ser2 = pd.Series(ser)

# Example 3: use index as a list to convert series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"],
                index =['r1', 'r2', 'r3', 'r4', 'r5','r6','r7'])

# Example 4: convert list to pandas series using multiple list 
list = [['Spark'], ['Pyspark'], ['pandas'], ['Oracle'],
         ['Java'], ['Pythan'], ['NumPy'], ['Hyerion']]
ser2 = pd.Series((v[0] for v in list))

# Example 5: create pandas series from list
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
ser2 = pd.Series(ser, index=['r1', 'r2','r3', 'r4', 'r5','r6','r7'])

# Example 6: create Pandas Series with define indexes
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
index = ['r1', 'r2','r3', 'r4', 'r5','r6','r7']
ser2 = pd.Series(ser, index = index)

2. Convert List to Pandas Series

To convert the list to a Pandas series use pandas.Series() function. This is one of the basic ways for creating a series from a list in Pandas.


# convert python list to pandas series
listObj = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
ser = pd.Series(listObj)
print(ser)

Yields the same output as above.


# Output
0       Java
1      Spark
2    PySpark
3     Pandas
4      NumPy
5     Python
6     Oracle
dtype: object

While creating a Series from the list, you can also assign a name to it.


# Assign name to Series
ser = pd.Series(listObj, name='Technologies')
print(ser)

This assigns a ‘Technologies’ name to the Series.

3. Conver to Series from List of Values & Index

We can also use the index as a list while converting to series. Here I will take the index as a list with values 'r1','r2','r3','r4','r5','r6','r7'.


# use index as a list to convert series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"],
                index =['r1', 'r2', 'r3', 'r4', 'r5','r6','r7'])
print(ser)

Yields below output.


r1       Java
r2      Spark
r3    PySpark
r4     Pandas
r5      NumPy
r6     Python
r7     Oracle
dtype: object

4. Convert List to Series Using Multiple List

If you have a list of lists and you can easily convert it to a pandas Series by iterating over them using for loop and pass it into Series() function, it will return a series based on its index.


# convert list to pandas series using multiple list 
list = [ ['Spark'], ['Pyspark'], ['pandas'], ['Oracle'],
         ['Java'], ['Pythan'], ['NumPy'], ['Hyerion'] ]
           
# create Pandas Series
ser2 = pd.Series((i[0] for i in list))
print(ser2) 

Yields below output.


0      Spark
1    Pyspark
2     pandas
3     Oracle
4       Java
5     Pythan
6      NumPy
7    Hyerion
dtype: object

6. Complete Example For Convert Python List to Series


import pandas as pd

# convert python list to pandas series
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
ser2 = pd.Series(ser)

# use index as a list to convert series
ser = pd.Series(['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"],
                index =['r1', 'r2', 'r3', 'r4', 'r5','r6','r7'])
print(ser)

# convert list to pandas series using multiple list 
list = [['Spark'], ['Pyspark'], ['pandas'], ['Oracle'],
         ['Java'], ['Pythan'], ['NumPy'], ['Hyerion']]
ser2 = pd.Series((v[0] for v in list))
print(ser2) 

# create pandas series from list
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
ser2 = pd.Series(ser, index=['r1', 'r2','r3', 'r4', 'r5','r6','r7'])
print(ser2)

# create Pandas Series with define indexes
ser = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
index = ['r1', 'r2','r3', 'r4', 'r5','r6','r7']
ser2 = pd.Series(ser, index = index)
print(ser2)

7. Conclusion

In this article, I have explained how to convert a Python list to pandas series using pandas.Series() function and several other ways with examples.

Happy Learning !!

References

Leave a Reply

You are currently viewing How to Convert List to Pandas Series