Answers for "pandas update with condition"

2

pandas update with condition

import pandas as pd
import numpy as np

df = pd.DataFrame({'value':np.arange(1000000)})

# Solution 1 - Fastest :
df['value'] = np.where(df['value'] > 20000, 0, df['value'])

# Solution 2:
df.loc[df['value'] > 20000, 'value'] = 0

# Solution 3:
df['value'] = df['value'].mask(df['value'] > 20000, 0)

# Solution 4 - Slowest, note that df.where applies where condition is wrong:
df['a'] = df.where(df.a <= 20000, 0)
Posted by: Guest on December-15-2020
0

pandas filter and change value

df.loc[df['dollars_spent'] > 0, 'purchase'] = 1
Posted by: Guest on November-02-2020

Code answers related to "pandas update with condition"

Python Answers by Framework

Browse Popular Code Answers by Language