Unexpected Python error

Leonardo
Leonardo Registered Posts: 14 ✭✭✭✭

Dear DSS,

I develop a Python recipe which I validate in my Pycharm and works fine. The recipe extracts info from big tabulated data and does some processing, before ML modeling.

However, I get the following error (attached) at a line of code which uses loc and essentially gets all rows across only the second to the last column of my dataframe (takes out the first column).

# Get out duplicate columns (i.e. patient_id here)

df_new2=df_new2.loc[:,'Max_Pt_date':'Q4']

As mentioned, the "loc" attribute works fine in my Pycharm (and it is a very standard operation).

Any ideas what I should do? Thank you!

Best Answer

  • JuanE
    JuanE Dataiker, Registered Posts: 45 Dataiker
    edited July 17 Answer ✓

    Hello,

    There is no problem with the loc method. The KeyError is telling you that the ‘Q4’ column cannot be found in the dataframe. You should check that is indeed the case.

    Having said that, if you want to slice your dataframe by selecting all columns except the first, you could do something like this:

    df.drop(columns=df.columns[0])

    This will work regardless of your dataframe column names.

Answers

Setup Info
    Tags
      Help me…