3

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!

8
  • Will you please show a sample of your raw CSV file, including the header and some rows? Commented Jan 25, 2022 at 23:41
  • 1
    From the docs (emphasis mine): "names: List of column names to use. If the file contains a header row, then you should explicitly pass header=0 to override the column names. Duplicates in this list are not allowed." Commented Jan 25, 2022 at 23:43
  • 1
    @richardec -- I edited the original question. Commented Jan 25, 2022 at 23:45
  • @G.Anderson -- passing the header parameter results in the same behavior. Commented Jan 25, 2022 at 23:48
  • 1
    I'm not able to reproduce, the given file produced the intended result Commented Jan 25, 2022 at 23:58

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

While the docs say explicitly pass header=0, I think they really mean pass header=[actual header row number]. This allows you to still skip any text above the header, while still replacing the header column names with the desired ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.