[Python] Pandas KeyError that Unexplainable

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a beginner in the Machine learning scope and currently i'm working with pandas. Im using macOs, Anaconda where i create the environment then launch Jupyter notebook

    i encountered the same problem with different Dataframes.

    1/First DataFrame i did a small dataset like this

    df=pd.read_csv('motor.csv')
    df


    DataSet

    listing the columns names:

    df.columns


    gives --> Index(['Cars ', 'Bicycles', 'Truck', 'Motobikes'], dtype='object')

    however, when i want to print the values of any columns i get this Error

    df['Cars']


    [Error 1], however, i did the same for lets say Truck column it will work

    NOW i moved to a real ML project: with the same frustrating error

    I have this dataset that i loaded to my proj

    cols=['fLength','fWidth','fSize','fConc','fConc1','fAsym','fM3Long','fM3Trans','fAlpha','fDist','class']


    df=pd.read_csv('magic04.data', names=cols)


    Dataset2

    df['class'].unique()


    output: array(['g', 'h'], dtype=object)

    Now i want to convert these strings to integers

    df['class']=(df['class' == "g"]).astype(int)


    and boom i met with the same error Error2

    Did i do wrong with my code or is it Anaconda ?

    Continue reading...

Compartilhe esta Página