Home > database >  Passing column names to Pandas read_csv() function
Passing column names to Pandas read_csv() function

Time:01-26

I have a csv file that looks as follows:

,time,o,h,l,c
0,2021-03-17 09:30:00,101.25,103.98,90.17,101.78
1,2021-03-17 09:45:00,102.83,107.49,95.22,101.93
2,2021-03-17 10:00:00,100.92,102.90,97.19,102.80

I'm reading in this data using the read_csv function, as follows:

cols = ['indx', 'timestamp', 'open', 'high', 'low', 'close']
df = pd.read_csv('prices.csv', names=cols)

The resulting dataframe looks as follows:

    Unnamed: 0  time                o       h       l       c
0   0           2021-03-17 09:30:00 101.25  103.98  90.17   101.78
1   1           2021-03-17 09:45:00 102.83  107.49  95.22   101.93
2   2           2021-03-17 10:00:00 100.92  102.90  97.19   102.80

Passing the names parameter seems to have no effect on the column names.

EDIT:

Passing the header parameter (per the docs) results in the same behavior:

cols = ['indx', 'timestamp', 'open', 'high', 'low', 'close']
df = pd.read_csv('prices.csv', header=0, names=cols)

Do I have to pass another parameter to the read_csv function to customize the column names?

Thanks!

CodePudding user response:

My guess is that you need to pass header=0 to read_csv:

df = pd.read_csv('prices.csv', names=cols, header=0)

Output:

>>> df
   indx            timestamp    open    high    low   close
0     0  2021-03-17 09:30:00  101.25  103.98  90.17  101.78
1     1  2021-03-17 09:45:00  102.83  107.49  95.22  101.93
2     2  2021-03-17 10:00:00  100.92  102.90  97.19  102.80
  •  Tags:  
  • Related