I'm trying to building a lambda function within an apply that:
- checks whether a inputted list is inside of another list
- If it exists, append a value to another column in the same dataframe
Example:
initial dataframe:
id list_1 list_2
1 [1,2,3] []
2 [1,2,4] []
3 [1,3,4] []
Now, I want to check if [1,2] is present within list_1. If it is, append test to list_2 lists.
final_dataframe:
id list_1 list_2
1 [1,2,3] ['test']
2 [1,2,4] ['test']
3 [1,3,4] []
Here is my first attempt:
df.apply(lambda row: row['list_2'].append(['test' if all(elem in [1,2] for elem in row['list_1'])]), axis = 1)
But i'm getting an invalid systax error. I feel like this is probably pretty simple but I can't figure out what the issues
Here is the full error:
File "<ipython-input-126-f45b74393598>", line 3
movies_test.apply(lambda row: row['displayable'].append(['comedy_drama' if all(elem in ['comedy','drama'] for elem in row['test'])]), axis = 1)
^
SyntaxError: invalid syntax
else- you can't do that