Pandas Data Series: Convert a Panda module Series to Python list and it’s type
Pandas: Data Series Exercise-2 with Solution
Write a Pandas program to convert a Panda module Series to Python list and it’s type.
Sample Solution :
Python Code :
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print("Pandas Series and type")
print(ds)
print(type(ds))
print("Convert Pandas Series to Python list")
print(ds.tolist())
print(type(ds.tolist()))
Sample Output:
Pandas Series and type 0 2 1 4 2 6 3 8 4 10 dtype: int64 <class 'pandas.core.series.Series'> Convert Pandas Series to Python list [2, 4, 6, 8, 10] <class 'list'>
Explanation:
ds = pd.Series([2, 4, 6, 8, 10]): This code creates a Pandas Series object named 'ds' containing a sequence of five integers: 2, 4, 6, 8, and 10.
print(type(ds)): This statement prints the type of the 'ds' object using the type() function, which will be <class 'pandas.core.series.Series'>. This confirms that the object is indeed a Pandas Series.
print(ds.tolist()): This statement prints the contents of the Series object as a Python list using the tolist() method.
print(type(ds.tolist())): Finally print() function prints the type of the resulting list object using the type() function, which will be <class 'list'>.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to create and display a one-dimensional array-like object containing an array of data using Pandas module.
Next: Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/pandas/python-pandas-data-series-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics