Python Forum
How to change row 2 to column header within a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change row 2 to column header within a dataframe
#1
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.
Reply
#2
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/st...d_csv.html

Take a look at the 'header' parameter.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#3
(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/st...d_csv.html

Take a look at the 'header' parameter.

Thank you for the prompt solution!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 328 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 748 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 645 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 962 Jan-30-2023, 01:19 PM
Last Post: nafshar
  Change a numpy array to a dataframe Led_Zeppelin 3 1,128 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 849 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,563 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,616 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Python create a spreadsheet with column and row header ouruslife 4 1,650 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 11,458 Jul-08-2022, 08:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020