I have this dataframe:
id class text
1 ["oil","water"] text1
2 ["oil"] text2
3 ["sun","water","earth"] text3
and I have a list of all possible class using this code:
import ast
df.class.map(ast.literal_eval).explode().value_counts()
oil
water
sun
earth
I want to create a new dataframe with all classes as column names and set 1 if the column name corresponds to the class column:
id class text oil water sun earth
1 ["oil","water"] text1 1 1 0 0
2 ["oil"] text2 1 0 0 0
3 ["sun","water","earth"] text3 0 1 1 1
I tried:
f = df.explode('class').pivot(columns='class', index='text', values='text').notnull().astype(int)
But the columns name is not splited correctly