Home > Back-end >  Rename pandas Dataframe Column With Data Under it
Rename pandas Dataframe Column With Data Under it

Time:10-27

I have a dataframe like this :

df_data
column1 column2 column3
January February March
100 200 300

I Want to change the column name to something like this by increasing the contents of the dataframe from the first index

df_data
January February March
100 200 300

CodePudding user response:

Convert first line to columns names and then filter out first line of DataFrame by DataFrame.iloc:

df.columns = df.iloc[0]
df = df.iloc[1:].rename_axis(columns=None)
print (df)
  January February March
1     100      200   300

For one line solution:

df = df.rename(columns=df.iloc[0]).iloc[1:].rename_axis(columns=None)
print (df)
  January February March
1     100      200   300
  • Related