Search code examples
pythondataframedictionarycategories

How do I use a dictionary to add a category column in a dataframe?


So I have a dictionary:

dict = {'apple':'fruit', 'grape':'fruit', 'chickpea':'beans','coffee cup':'tableware'}

And I was wondering how to use it to make a new column that categorizes this dataframe with strings of text:

Item Cost
apple from happy orchard 15
grape from random vineyard 20
chickpea and black bean mix 10
coffee cup with dog decal 14

Into something like

Item Cost Category
apple from happy orchard 15 fruit
grape from random vineyard 20 fruit
chickpea and black bean mix 10 beans
coffee cup with dog decal 14 tableware

Any help would be appreciated!

I've made my dictionary, and I've tried

dict = {'apple':'fruit', 'grape':'fruit', 'chickpea':'beans','coffee cup':'tableware'}
df[category] = 'a'
df['category'] = df['Item'].map(dict)

but I don't know what to do with the string


Solution

  • You are on the right path, it's just left with a few tweaks. You can use apply to map each item in the Item column to its respective category like this:

    # Your dictionary
    dict = {'apple': 'fruit', 'grape': 'fruit', 'chickpea': 'beans', 'coffee cup': 'tableware'}
    
    # Your DataFrame
    data = {'Item': ['apple from happy orchard', 'grape from random vineyard', 'chickpea and black bean mix', 'coffee cup with dog decal'],
            'Cost': [15, 20, 10, 14]}
    
    df = pd.DataFrame(data)
    
    # Mapping the 'Item' column to 'Category'
    df['Category'] = df['Item'].apply(lambda x: next((value for key, value in category_dict.items() if key in x), None))
    
    print(df)