0

I am trying to map data using a list instead of a string

If the number is inside the list, then map it to xyz

Any way I can do this using a list?

import pandas as pd

df = pd.DataFrame({'INDICATOR': ['0001024561', 'B', '0001024561', 'D'], 'VALUE': [10, 9, 8, 7]})

company_dict = {[
    '0001024561',
    '0001024576',
    ]: 'xyz'}

df['NEW_VALUE'] = df['INDICATOR'].map(company_dict)

print(df)

Error:

TypeError: unhashable type: 'list' 

1 Answer 1

1

IICU:

Use dict

company_dict = {'0001024561':'xyz','0001024576':'xyz'}
df['NEW_VALUE'] = df['INDICATOR'].map(company_dict)

Using list

lst=['0001024561','0001024576']
df.loc[df['INDICATOR'].isin(lst),'NEW_VALUE']='xyz'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for accepting answer, could you please press the up arrow so that people seeking answers can confidently use solution?

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.