One could use .loc (combined to .str[0])
With:
df = pd.DataFrame(dict(ingredient_name=['Cheese','Cheese','Egg'],
ingredient_method=[['camembert', 'pkg'],
['cream', 'pastueri'],
['raw', 'scrambled']]))
Do:
#Initialize consolidated_name with None for instance
df['consolidated_name'] = [None]*len(df) #Not mandatory, will fill with NaN if not set
#Use .loc to get the rows you want and .str[0] to get the first elements
_filter = df.ingredient_name=='Cheese' #Filter you want to
df.loc[_filter,'consolidated_name'] = df.loc[_filter,'ingredient_method'].str[0]
Result:
print(df)
ingredient_method ingredient_name consolidated_name
0 [camembert, pkg] Cheese camembert
1 [cream, pastueri] Cheese cream
2 [raw, scrambled] Egg None
Note
#1
If you want to consolidate all the duplicated ingredients you can filter with the following:
_duplicated = df.ingredient_name[df.ingredient_name.duplicated()]
_filter = df.ingredient_name.isin(_duplicated)
The use of .loc is unchanged see next example:
df = pd.DataFrame(dict(ingredient_name=['Cheese','Cheese','Egg','Foo','Foo'],
ingredient_method=[['camembert', 'pkg'],
['cream', 'pastueri'],
['raw', 'scrambled'],
['bar', 'taz'],
['taz', 'bar']]))
_duplicated = df.ingredient_name[df.ingredient_name.duplicated()]
_filter = df.ingredient_name.isin(_duplicated)
df.loc[_filter,'consolidated_name'] = df.loc[_filter,'ingredient_method'].str[0]
print(df)
ingredient_method ingredient_name consolidated_name
0 [camembert, pkg] Cheese camembert
1 [cream, pastueri] Cheese cream
2 [raw, scrambled] Egg NaN
3 [bar, taz] Foo bar
4 [taz, bar] Foo taz
#2
If you want you can initialize with ingredient_name:
df['consolidated_name'] = df.ingredient_name
Then do your stuff:
_duplicated = df.ingredient_name[df.ingredient_name.duplicated()]
_filter = df.ingredient_name.isin(_duplicated)
df.loc[_filter,'consolidated_name'] = df.loc[_filter,'ingredient_method'].str[0]
print(df)
ingredient_method ingredient_name consolidated_name
0 [camembert, pkg] Cheese camembert
1 [cream, pastueri] Cheese cream
2 [raw, scrambled] Egg Egg #Here it has changed
3 [bar, taz] Foo bar
4 [taz, bar] Foo taz
i's androw's in your code. Further, it seems likeset_valuemethod is not an in-place operation so yourdfwill not change at all.