My goal is to enter a 1 whenever a string in the 'SAD' column matches a column name, and 0 otherwise.
So we have this dataframe:
df = pd.DataFrame({'SAD': ['Game;Pool;Read;lol','Game;Read'], 'Game':[0,0], 'Pool': [0, 0], 'Read':[0, 0], 'Drink' : [0, 0]})
I have tried this: Splitting then assigning:
l= df["SAD"].apply(lambda x: x.split(";"))
for i in l:
for x in i:
if x in df.columns:
df[x]=1
print(df)
However this replaced the entire dataframe with 1 at the first sight of a string that matches:
SAD Game Pool Read Drink
0 Game;Pool;Read;lol 1 1 1 0
1 Game;Read 1 1 1 0
The desired output would be:
SAD Game Pool Read Drink
0 Game;Pool;Read;lol 1 1 1 0
1 Game;Read 1 0 1 0