+1 vote
in Programming Languages by (8.1k points)

I have a dataframe with header. How can I remove the header of the dataframe?

E.g.

From

>>> df
   aa  bb  cc
0   1   4   7
1   2   5   8
2   3   6   9
 

To

>>> df
   0  1  2
0  1  4  7
1  2  5  8
2  3  6  9
 

1 Answer

0 votes
by (15.9k points)

Here is one of the approaches to remove the header of a pandas dataframe:

  • First convert dataframe to numpy matrix using values
  • Then convert numpy matrix to pandas dataframe using from_records() 
Here is your example:

>>> import pandas as pd
>>> df=pd.DataFrame({'aa':[1,2,3],'bb':[4,5,6],'cc':[7,8,9]})
>>> df
   aa  bb  cc
0   1   4   7
1   2   5   8
2   3   6   9
>>> df.values
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
>>> df1=pd.DataFrame.from_records(df.values)
>>> df1
   0  1  2
0  1  4  7
1  2  5  8
2  3  6  9
...