How to use to_records() method In Python Pandas

This is a pandas module method used to convert multiindex dataframe into each record and display.

Syntax:

dataframe.to_records()

Example:

Python3




import pandas as pd
 
# create DataFrame multiIndex
data = pd.MultiIndex.from_tuples([('Web Programming', 'php', 'sub1'),
                                  ('Scripting', 'python', 'sub2'),
                                  ('networks', 'computer network', 'sub3'),
                                  ('architecture', 'computer organization', 'sub4'),
                                  ('coding', 'java', 'sub5')],
                                 names=['Course', 'Subject name', 'subject id'])
 
# create dataframe with student marks
 
data = pd.DataFrame({'ravi': [98, 89, 90, 88, 93],
                     'reshma': [78, 89, 80, 98, 63],
                     'sahithi': [78, 89, 80, 98, 63]},
                    index=data)
 
pd.DataFrame(data.to_records())


Output:



How to Flatten MultiIndex in Pandas?

In this article, we will discuss how to flatten multiIndex in pandas.

Similar Reads

Flatten all levels of MultiIndex:

In this method, we are going to flat all levels of the dataframe by using the reset_index() function....

Flatten specific levels of MultiIndex

...

Using to_records() method

...

Contact Us