Pandas split multivalue column into separate columns - get_dummies



examples/pandas/multivalue_get_dummies.py
import pandas as pd

dfin = pd.read_csv('multivalue.csv')
print(dfin)
print()
fruits_dummies = dfin['Fruits'].str.get_dummies(',').astype(bool)
df = pd.concat([dfin, fruits_dummies], axis=1)
print(df)

  MyText              Fruits
0    Joe               Apple
1   Jane        Apple,Banana
2   Mary  Banana,Peach,Melon
3    Bob                 NaN
4   Zane               Melon

  MyText              Fruits  Apple  Banana  Melon  Peach
0    Joe               Apple   True   False  False  False
1   Jane        Apple,Banana   True    True  False  False
2   Mary  Banana,Peach,Melon  False    True   True   True
3    Bob                 NaN  False   False  False  False
4   Zane               Melon  False   False   True  False