Add value or text to column a if condition in column b is met pandas

max

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

Scott Boston

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.

edited at
0

Comments

0 comments
Login to comment

Related

Fill rows in column A with value of column B if condition in column A is met

Add quantity to pandas column if row condition is met

When a condition is met in a pandas column return the value of another column

Trying to return value from column in pandas if condition in another column is not met

Pandas: Replace value of column with previous row value if condition is met

Add column value in panda data frame if certain condition is met

pandas copy value from one column to another if condition is met

pandas add value to new column based on condition

Add character to column based on ascending order of another column if condition met pandas

Add a column based on condition met in other columns

Renaming column in pandas dataframe if condition is met

Find value in column after a certain condition is met

Replace value on specific column if condition is met

Pyspark apply function to column value if condition is met

Change the Value of Column When Condition is met

Update row value with column name if condition is met

How to assign a set value to a column if a condition is met?

Pandas count column a if column b condition

Extract value in column B when Value in column A is met in R

R: Find a value in rows met a condition, add certain numbers to the value, and then create a message in that row in another column

pandas apply function on column only if condition on another column is met

Pandas function to perform a calculation on one column, if condition is met on a different column

Pandas: sum column until condition met in other column

Python Pandas: Apply method to column if condition met in another column

Add new column in pandas dataframe using empty string or the value from column A depending on the value on column B

get a value from another column if condition for this column is met

Pandas - Add value to a Column on a For

add column calculation with condition in pandas

Pandas: Add new column and assigning value from another dataframe by condition