Floris van Eijk
Floris van Eijk

Reputation: 1

Checking dataframe columns with dictionary

So I have a dataframe, from which I want to check values of two columns (say column A and B) which both contain strings.
I also have a dictionary which contains keys and lists as values (basically functions as a checklist).

What I want to do is to check if the value in column B is in the dictionary values (dictionary contains lists of values), where the value in column A corresponds to a key in the dictionary. Then what I need is to add to a column C whether this returns True or False

The only way I can think of doing this is with a lot of nested loops, which is what I need to avoid because it will take way too long.

As an example:

df_dict = {'fruit':['apple', 'apple', 'banana', 'banana', 'orange'], 'feature':['sweet', 'green', 'sweet', 'red', 'square']}
df = pd.DataFrame.from_dict(df_dict)

check_dict = {'apple': ['sweet', 'green'], 'banana':['sweet', 'yellow'], 'orange':['round', 'orange']}

I have a dataframe with column 'fruits' containing fruits, and column 'feature' containing a feature of that fruit. So for example 'apple' has the feature 'sweet'. Now I have another dictionary which contains for each fruits which features it should contain. Now what I want is to add a third column to my dataframe, which shows whether the row corresponds with the dictionary key and value.

Upvotes: 0

Views: 706

Answers (1)

Z Li
Z Li

Reputation: 4318

Say you want to create column 'c' with dictionary d

df = pd.DataFrame({'a':[1,2,3], 'b':[2,3,6]})
d = {1:2, 2:4}
df['c'] =  df['b'] == df['a'].apply(lambda x: d.get(x))
    a   b   c
0   1   2   True
1   2   3   False
2   3   6   False

Upvotes: 1

Related Questions