Home > Mobile >  Pandas reading csv from url, read first row and set header as second
Pandas reading csv from url, read first row and set header as second

Time:12-02

I have a csv where the first row contains the version number and the 2nd row contains the headers.

Is it possible to read the first row (save the version number to a variable), then create the data frame using the following row as the header?

data = pd.read_csv(url, header=1, encoding='windows-1252')

csv example

enter image description here

CodePudding user response:

Use nrows parameter for read only first row and select columns and then for DataFrame excluded this data use skiprows=1 parameter:

c = pd.read_csv(url, nrows=0, encoding='windows-1252').columns
last = c[0]
date = c[1]

data = pd.read_csv(url, skiprows=1, encoding='windows-1252')

CodePudding user response:

You are already doing it, just have a separate reading for the version

first_row = pd.read_csv(url, nrow=0, encoding='windows-1252')
last_updated = first_row.columns[1]
  • Related