pandas 替换熊猫数据框中的特定范围的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46168450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:26:07  来源:igfitidea点击:

Replace a specific range of values in a pandas dataframe

pythonpandasdataframe

提问by jayko03

I have big data set and there are tons of values which are way over average. For example,

我有大数据集,有很多值远远超过平均水平。例如,

    A         B
1  'H'       10
2  'E'    10000
3  'L'       12
4  'L'        8
5  'O'       11

and I want to set B2cell as 0 and I tried this,

我想将B2单元格设置为 0,我试过了,

df['B'] = df['B'].replace([df['B'] > 15], 0)

But didn't get any luck. How can make my data frame like this,

但没有得到任何运气。如何让我的数据框像这样,

    A         B
1  'H'       10
2  'E'        0
3  'L'       12
4  'L'        8
5  'O'       11

Thank you!

谢谢!

回答by jezrael

You are really close - instead of replace, use mask:

你真的很接近 - 而不是replace,使用mask

df['B'] = df['B'].mask(df['B'] > 15, 0)
print (df)
     A   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)
     A   B
1  'H'  10
2  'E'   0
3  'L'  12
4  'L'   8
5  'O'  11

If you want replace some range:

如果你想替换一些范围:

df['B'] = np.where(df['B'].between(8,11), 0, df['B'])
print (df)
     A      B
1  'H'      0
2  'E'  10000
3  'L'     12
4  'L'      0
5  'O'      0

回答by DYZ

Another alternative:

另一种选择:

df.loc[df['B'] > 15, 'B'] = 0
#   df
#    B
#0  10
#1   0
#2  12
#3   8
#4  11