python - How to assign values based on multiple columns in pandas? -
is there elegant way assign values based on multiple columns in dataframe in pandas? let's have dataframe 2 columns: fruittype , color.
import pandas pd df = pd.dataframe({'fruittype':['apple', 'banana','kiwi','orange','loquat'], 'color':['red_black','yellow','greenish_yellow', 'orangered','orangeyellow']})
i assign value of third column, 'isyellowseedless', based on both 'fruittype' , 'color' columns.
i have list of fruits consider seedless, , check color column see if contains str "yellow".
seedless = ['banana', 'loquat']
how string elegantly?
this attempt didn't work:
df[(df['fruittype'].isin(seedless)) & (culture_table['color'].str.contains("yellow"))]['isyellowseedless'] = true
or can try
df['isyellowseedless']=df.loc[df.fruittype.isin(seedless),'color'].str.contains('yellow') df out[546]: color fruittype isyellowseedless 0 red_black apple nan 1 yellow banana true 2 greenish_yellow kiwi nan 3 orangered orange nan 4 orangeyellow loquat true
Comments
Post a Comment