Skip to content Skip to sidebar Skip to footer

How To Create Boolean Columns Based On Column Values In Another Dataframe

Lets say I have two pandas DataFrames, df1 and df2, one containing the names and age of people, and the other detailing what they are studying. What is an efficient way to join the

Solution 1:

Use get_dummies with max, then join and replace missing values only for columns from df22:

s = df2.set_index('name')['studies']
df22 = pd.get_dummies(s, prefix_sep='', prefix='', dtype=bool).max(level=0)
df = df1.join(df22, on='name').fillna(dict.fromkeys(df22.columns, False))
print (df)
    name  age  commerce  education    law  science
0   John   24     False       True  False     True
1  Kelly   49     False      False  False     True
2  Gemma   18     False      False  False    False
3    Bob   29      True      False   True    False

Post a Comment for "How To Create Boolean Columns Based On Column Values In Another Dataframe"