I have the following sample DataFrame and List (extracted from df).
import pandas as pd
color_list = ['green','blue','red','yellow','black']
df = pd.DataFrame({'object': ['car','plane','tree','house','phone'],
'colors': ['red, blue',
'red, yellow, black',
'black',
'green, blue',
'yellow, green, blue']})
I managed to create a for loop that correctly checks if a certain colour (from color_list) is present in the colors column. It then creates a new column that evaluates that condition and assigns True or False to each row. The code is the following:
idx = df.columns.get_loc('colors') + 1
for i in range(len(color_list)):
df.insert(loc= idx + i,
column='has ' + color_list[i],
value = (df['colors'].str.contains(color_list[i], case=False, na=False)))
It outputs the following:
object colors has green ... has red has yellow has black
0 car red, blue False ... True False False
1 plane red, yellow, black False ... True True True
2 tree black False ... False False True
3 house green, blue True ... False False False
4 phone yellow, green, blue True ... False True False
Which is correct, the question is can i pass a condition to the value argument of pd.insert?
I want to replace True values with 'has'+(name of the column)' and False with something like 'doesn't have'+(name of column). can this be done in the same loop?
Thanks,