Pandas split multivalue column into separate columns



examples/pandas/multivalue.csv
MyText,Fruits
Joe,Apple
Jane,"Apple,Banana"
Mary,"Banana,Peach,Melon"
Bob,
Zane,"Melon"


examples/pandas/multivalue.py
import pandas as pd

df = pd.read_csv('multivalue.csv')
print(df)
print()

values = ["Apple", "Banana", "Peach", "Melon"]
for value in values:
    df[value] = df.apply(lambda row: pd.notnull(row['Fruits']) and value in row['Fruits'].split(','), 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  Peach  Melon
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  False   True