tolist(): List from Pandas DataFrame

Pandas

We can create one list from DataFrame
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,60,30],
         'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict) # dataframe 
my_list=df.values.tolist() # creates a list 
print(type(my_list)) # <class 'list'>
This will hold all our dataframe data ( but without column names )
print(my_list)
Output
[['Ravi', 1, 80, 80], ['Raju', 2, 40, 70], 
['Alex', 3, 70, 40], ['Ron', 4, 70, 50], 
['King', 5, 60, 60], ['Jack', 6, 30, 30]]

Adding column names to the list.

We used list insert method to add the column names.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,60,30],
         'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict) # dataframe 
my_list=df.values.tolist() # creates a list 
my_list.insert(0,df.columns) # adding column names to list
print(my_list)
Output
[Index(['NAME', 'ID', 'MATH', 'ENGLISH'], dtype='object'), 
['Ravi', 1, 80, 80], ['Raju', 2, 40, 70], 
['Alex', 3, 70, 40], ['Ron', 4, 70, 50], 
['King', 5, 60, 60], ['Jack', 6, 30, 30]]
tolist() is used to create LIST from DataFrame to create PDF table
Pandas DataFrame describe() head() to_string() rename()
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer