Pandas to List
To convert a Pandas DataFrame or Series to a list, you can use the .tolist()
method. Here’s how you can do it for both DataFrames and Series:
For DataFrame:
import pandas as pd
# Sample DataFrame
data = {
'Column1': [1, 2, 3, 4, 5],
'Column2': ['A', 'B', 'C', 'D', 'E']
}
df = pd.DataFrame(data)
# Convert DataFrame to a list of lists
list_of_lists = df.values.tolist()
print(list_of_lists)
For Series:
import pandas as pd
# Sample Series
data = pd.Series([1, 2, 3, 4, 5])
# Convert Series to a list
list_from_series = data.tolist()
print(list_from_series)
In both cases, the .tolist()
method will convert the DataFrame or Series into a Python list. For DataFrames, it creates a list of lists where each row of the DataFrame becomes a separate list. For Series, it directly converts the Series values into a list.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks