In this article, we will discuss how to convert a Pandas series to a Python List and it’s type. This can be done using the tolist() method.
Example 1:
Python3
import pandas as pd evenNumbers = [ 2 , 4 , 6 , 8 , 10 ] evenNumbersDs = pd.Series(evenNumbers) print ( "Pandas Series and type" ) print (evenNumbersDs) print ( type (evenNumbersDs)) print ( "\nConvert Pandas Series to Python list" ) print (evenNumbersDs.tolist()) print ( type (evenNumbersDs.tolist())) |
Output:
In the above example, we are passing Panda series data as evenNumbers . As index is not given, by default 0 is taken
Example 2:
Python3
import pandas as pd import numpy as np data = np.array([ 'ant' , 'bat' , 'cat' , 'dog' ]) series = pd.Series(data,index = [ 100 , 101 , 102 , 103 ]) print (series) print ( type (series)) print ( "\nConvert Pandas Series to Python list" ) print (series.tolist()) print ( type (series.tolist())) |
Output:
In the above example, we are passing Panda series data as np.array([‘ant’,’bat’,’cat’,’dog’]).
Index as [100,101,102,103]
Example 3:
Python3
# Series from dictionary import pandas as pd import numpy as np marks = { 'Maths' : 100. , 'Physics' : 90. , 'Chemistry' : 85. } series = pd.Series(marks) print (series) print ( type (series)) print ( "\nConvert Pandas Series to Python list" ) print (series.tolist()) print ( type (series.tolist())) |
Output:
Example 4:
Python3
# Get First 3 student marks and # convert as list import pandas as pd series = pd.Series([ 100 , 90 , 80 , 90 , 85 ], index = [ 'Student1' , 'Student2' , 'Student3' , 'Student4' , 'Student5' ]) # retrieve the first three element print (series[: 3 ]) print (series[: 3 ].tolist()) print ( type (series[: 3 ].tolist())) |
Output: