pythonpandasdataframe

Why this Pandas DataFrame column operation fails?


This script works fine with Python 3.11 and Pandas 2.2:

import pandas as pd

df = pd.read_csv(f'test.csv', comment='#')
df['x1'] = df['x1']*8
# df['y1'] = df['y1']*8
print(df)

and prints:

   x1   y1
0   0    0
1  16    6
2  32   12

but fails when I uncomment df['y1'] = df['y1']*8 and produces KeyError: 'y1'. Why is that? 'y1' is a valid key. Here is the test.csv file:

# comment 
x1, y1
0, 0
2, 6
4, 12

Solution

  • Read your file with the skipinitialspace=True option of read_csv

    df = pd.read_csv(csv, skipinitialspace=True)
    

    Columns:

    ['x1', 'y1']