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.
Key Points –
- Use the
pd.Series()
function from the Pandas library to convert a Python list to a Pandas Series. - The resulting Series retains the order of elements from the original list while introducing indexing capabilities for efficient data manipulation.
- Conversion to a Pandas Series enables leveraging the extensive functionality provided by Pandas for data analysis, manipulation, and visualization.
- The elements of the list will become values in the resulting Series.
- Ensure compatibility of data types between the list elements and the Series, as Pandas Series can handle various data types but may perform type conversion if necessary.
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.
# Quick examples of convert list to series
# 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)
Convert List to Pandas Series
To convert the list to a Pandas series you can use pandas.Series()
function. This is one of the basic ways for creating a series from a list in Pandas.
import pandas as pd
# Convert python list to pandas series
listObj = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
print("Create list:\n", listObj)
ser = pd.Series(listObj)
print("After converting a Series from list:\n", ser)
Yields the same output as above.
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("After converting a Series from list:\n", ser)
This assigns a ‘Technologies’ name to the Series.
Conver to Series from List of Values & Index
You can also use the index as a list while converting to a series. Here I will take the index as a list of 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("After converting a Series from list:\n", ser)
Yields below output.
# Output:
# After converting a Series from list:
r1 Java
r2 Spark
r3 PySpark
r4 Pandas
r5 NumPy
r6 Python
r7 Oracle
dtype: object
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 a for
loop. To pass a list of lists 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("After converting a Series from list:\n", ser2)
Yields below output.
# Output:
# After converting a Series from list:
0 Spark
1 Pyspark
2 pandas
3 Oracle
4 Java
5 Pythan
6 NumPy
7 Hyerion
dtype: object
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)
Frequently Asked Questions on Convert List to Pandas Series
A Pandas Series is a one-dimensional labeled array capable of holding data of various types. It is similar to a Python list or one-dimensional NumPy array but with additional features and functionality.
To convert a Python list to a Pandas Series, you can use the pd.Series()
function provided by the Pandas library. Simply pass the list as an argument to this function.
You can specify custom index values when converting a list to a Pandas Series. You can provide the list of index values as a second argument to the pd.Series()
function.
Converting a list to a Pandas Series allows you to leverage the powerful data manipulation and analysis tools provided by the Pandas library. Series objects offer advanced functionality such as indexing, alignment, and arithmetic operations.
Ensure that the elements of the list are compatible with Pandas data types. Also, consider whether you need to specify custom index values or use the default integer index. Additionally, be aware that converting large lists to Series may consume memory, so consider memory constraints when working with large datasets.
Conclusion
In this article, I have explained how to convert a Python list to a Pandas series using pandas.Series()
function and several other ways with examples.
Happy Learning !!
Related Articles
- Pretty Print Pandas DataFrame or Series
- Change the Index Order in Pandas Series
- Check Values of Pandas Series is Unique
- Convert Pandas Series to NumPy Array
- Add Column Name to Pandas Series
- Remove NaN From Pandas Series
- Pandas Series filter() Function
- Get Series Index in Pandas
- Pandas Series any() Function
- Use pandas.to_numeric() Function
- Pandas Series.quantile() Function
- Pandas Series.shift() Function
- Pandas convert Series to string
- Pandas Series.diff() Function
- Pandas convert Series to dictionary
- How to convert the Pandas Series to list?