python - replace specific range of values in data frame pandas -
i have big data set , there tons of values way on average. example,
b 1 'h' 10 2 'e' 10000 3 'l' 12 4 'l' 8 5 'o' 11
and want set b2
cell 0 , tried this,
df['b'] = df['b'].replace([df['b'] > 15], 0)
but didn't luck. how can make data frame this,
b 1 'h' 10 2 'e' 0 3 'l' 12 4 'l' 8 5 'o' 11
thank you!
you close - instead replace
use mask
:
df['b'] = df['b'].mask(df['b'] > 15, 0) print (df) b 1 'h' 10 2 'e' 0 3 'l' 12 4 'l' 8 5 'o' 11
alternative:
df['b'] = np.where(df['b'] > 15, 0, df['b']) print (df) b 1 'h' 10 2 'e' 0 3 'l' 12 4 'l' 8 5 'o' 11
if want replace range:
df['b'] = np.where(df['b'].between(8,11), 0, df['b']) print (df) b 1 'h' 0 2 'e' 10000 3 'l' 12 4 'l' 0 5 'o' 0
Comments
Post a Comment