1

my dataframe looks like this:

    PD_DK    WARNSPEAK_DK
1    88         88
2    Nan         1    
3    88          1
4    1           1

here is my code(it's basically replacing 88 vavlue to 999999):

df2.replace({['PD_DK','WARNSPEAK_DK','TIME1','TIME2','TIME3','TIME4','WARNBEHAV_DK','TIME5','TIME6','TIME7',
            'TIME8','WARNEMOTION_DK','TIME9','TIME10','TIME11','TIME12']: 88}, 999999, inplace=True)
# to check
df2['PD_DK'].value_counts()

and I got the output such as:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [31], in <cell line: 1>()
----> 1 df2.replace({['PD_DK','WARNSPEAK_DK','TIME1','TIME2','TIME3','TIME4','WARNBEHAV_DK','TIME5','TIME6','TIME7',
      2             'TIME8','WARNEMOTION_DK','TIME9','TIME10','TIME11','TIME12']: 88}, 999999, inplace=True)
      3 df2['PD_DK'].value_counts()

TypeError: unhashable type: 'list'
1
  • Try to write it in a more clear way, it's difficult to get what you want your code to do Commented Oct 11, 2022 at 8:03

1 Answer 1

1

IIUC use if need specify columns by list:

cols = ['PD_DK','WARNSPEAK_DK','TIME1','TIME2','TIME3','TIME4','WARNBEHAV_DK','TIME5','TIME6',
        'TIME7','TIME8','WARNEMOTION_DK','TIME9','TIME10','TIME11','TIME12']
df2[cols] = df2[cols].replace({88: 999999})

Or create nested dictionaries in dict comprehension:

df2 = df2.replace({x:{88: 999999} for x in cols})
Sign up to request clarification or add additional context in comments.

Comments

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.