Python Forum
How to change row 2 to column header within a dataframe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to change row 2 to column header within a dataframe (/thread-29156.html)



How to change row 2 to column header within a dataframe - sparkt - Aug-20-2020

The following would read currency exchange rate data from an excel file online, but within the workbook, table name is written on row 1, column header is written on row 2 instead.

import pandas as pd 

erdata = pd.read_csv("https://www.censtatd.gov.hk/showtablecsv.jsp?TableID=124") 
print(erdata)
Since dataframe always identifies row 1 as column header, it would incorrectly identify only two columns in the workbook. And as usual the following drop function only remove row 2 which is the real column header.

print(erdata.drop(erdata.index[0]))
How to use codes (without manual downloading and excel editing) to remove the top row and make it correctly identify row 2 as the column header?
Any help would be very appreciated.


RE: How to change row 2 to column header within a dataframe - Marbelous - Aug-20-2020

Yes, there is an easy way built in to pandas to do exactly that. Have you looked at the docs???

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

Take a look at the 'header' parameter.


RE: How to change row 2 to column header within a dataframe - sparkt - Aug-20-2020

(Aug-20-2020, 03:42 PM)Marbelous Wrote: Yes, there is an easy way built in to pandas to do exactly that. Have you looked at the docs???

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

Take a look at the 'header' parameter.

Thank you for the prompt solution!