Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pandas split multivalue column into separate columns

{% embed include file="src/examples/pandas/multivalue.csv)

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