I keep getting nan for this, so hoping for a quick fix.
df:
cola colb
5 30
10 95
I'd like to add 20 to cola if colb is between 90 and 100, like this:
cola colb
5 30
30 95
Code i'm working with:
df['cola'] = df.loc[df['colb'].between(90,100), 'cola'] + 20
And bonus points - i have another column with text, i'd like to append a string if the condition is met, like this:
cola colb colc
5 30 some_text
30 95 some_text, condition_met
Code for this one that also returns nan is:
df['colc'] = df.loc[df['colb'].between(90,100), 'colc'] + 'condition_met'
Thanks
Use:
# Create input dataframe using copy to clipboard
df = pd.read_clipboard()
df['colc'] = 'some text'
# No need to iterate use pandas intrinsic data
# alignment with the addition assignment operator
df.loc[df['colb'].between(90,100), 'cola'] += 20
df.loc[df['colb'].between(90,100), 'colc'] += ', condition met'
df
Output:
cola colb colc
0 5 30 some text
1 30 95 some text, condition met
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments