Unexpected Python error

Solved!
Leonardo
Level 3
Unexpected Python error

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!

0 Kudos
1 Solution
JuanE
Dataiker

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.

View solution in original post

2 Replies
JuanE
Dataiker

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.

Leonardo
Level 3
Author

You are correct @JuanE , I was confused as I did not see the whole error message, until I posted it. Yes, the drop attribute is a good option. Thank you for your inputs, they helped a lot!

0 Kudos