Hariom Singh
Hariom Singh

Reputation: 3642

Convert first row of pandas dataframe to column name

I have a pandas dataframe

   0     1     2
0  pass fail  warning
1  50    12    34

I am trying to convert first row as column name something like this

   pass fail  warning
0  50    12    34

I am currently doing this by renaming the column name

 newdf.rename(columns={0: 'pass', 1: 'fail', 2:'warning'})

and then deleting the first row. Any better way to do it .

Upvotes: 15

Views: 29876

Answers (2)

Vidya P V
Vidya P V

Reputation: 481

For the dataframe DF, the following line of code will set the first row as the column names of the dataframe:

DF.columns = DF.iloc[0]

Upvotes: 25

jezrael
jezrael

Reputation: 863611

I believe need to add parameter to read_html:

df = pd.read_html(url, header=1)[0]

Or:

df = pd.read_html(url, skiprows=1)[0]

Upvotes: 6

Related Questions