I have a pandas dataframe, and I want to create a new column with values 'in list' or 'not in list', based on whether an entry in the first column is in a list. To illustrate I have a toy example below. I have a solution which works, however it seems very cumbersome and not very pythonic. I do also get a SettingWithCopyWarning. Is there a better or more recommended way to achieve this in python?
#creating a toy dataframe with one column
df = pd.DataFrame({'col_1': [1,2,3,4,6]})
#the list we want to check if any value in col_1 is in
list_ = [2,3,3,3]
#creating a new empty column
df['col_2'] = None
col_1 col_2
0 1 None
1 2 None
2 3 None
3 4 None
4 6 None
My solution is to loop through the first column and populate the second
for index, i in enumerate(df['col_1']):
if i in list_:
df['col_2'].iloc[index] = 'in list'
else:
df['col_2'].iloc[index] = 'not in list'
col_1 col_2
0 1 not in list
1 2 in list
2 3 in list
3 4 not in list
4 6 not in list
Which produces the correct result, but I would like to learn a more pythonic way of achieving this.