duektime201823
duektime201823

Reputation: 127

Perform multiple conditions on column

I have found myself looping through a Dataframe column to perform conditions, and based off those conditions the following result is adjacent to that cell. Is there a more efficient way to perform this?

For example I have the dataframe df:

  A          B
potato
orange
watermelon
lettuce


... etc.

I would perform the following code:

for i in range(len(df['A']):
    if df['A'].iloc[i] == 'watermelon':
        df['B'].iloc[i] = 'red'
    elif df['A'].iloc[i] == 'lettuce':
        df['B'].iloc[i] = 'green'
    elif .... : 

result:

  A          B
potato     brown
orange     orange 
watermelon red
lettuce    green

Upvotes: 1

Views: 38

Answers (1)

tdy
tdy

Reputation: 41457

Instead of a loop or apply(), use Series.replace() with a fruit:color dictionary:

fruit_color = dict(potato='brown', watermelon='red', lettuce='green')
df['B'] = df['A'].replace(fruit_color)

#             A       B
# 0      potato   brown
# 1      orange  orange
# 2  watermelon     red
# 3     lettuce   green

Upvotes: 2

Related Questions