-1

I am new in python. My problem is simple but I am struggling to solve this.

I am working with a simple dataframe. It has 2 columns Q and t. I attached a screeshot of my dataframe herewith.

My dtaframe

I want to set the Q and t as column names and remove the top header (0,1). Another thing is why the df.columns is showing rangeindex, how can I change that so it shoes the names of the columns.

I tried the following code

header = df.iloc[0]
print(header)
df = df[1:]
df.columns = header
df

I got the the first values of Q and t as header names instead of Q and t. df

I just want to remove the indexing for column names and set Q and t as column names.

Mahmuda
  • 1
  • 1
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Yevhen Kuzmovych Nov 11 '22 at 11:49
  • Does this answer your question? [Python Pandas Replacing Header with Top Row](https://stackoverflow.com/questions/31328861/python-pandas-replacing-header-with-top-row) – Yevhen Kuzmovych Nov 11 '22 at 11:51
  • When I tried that code( header = df.iloc[0] print(header) df = df[1:] df.columns = header ), the first value of Q and t are set as column names instead of Q and t. – Mahmuda Nov 11 '22 at 12:33

1 Answers1

0

In the following code, we removed the first row and rename the header row with it.

# get the first row and save in variable
header = df.iloc[0]

# slice the data leaving the header row
df = df[1:]

# rename the header row as the new dataframe's header
df = df.rename(columns=header)
Parham Alvani
  • 2,305
  • 2
  • 14
  • 25