Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert first row of pandas dataframe to column name

Tags:

python

pandas

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 .

like image 246
Hariom Singh Avatar asked Sep 26 '18 10:09

Hariom Singh


2 Answers

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]
like image 199
Vidya P V Avatar answered Oct 08 '22 12:10

Vidya P V


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]
like image 6
jezrael Avatar answered Oct 08 '22 12:10

jezrael