pandas 根据条件更新熊猫数据框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33274628/
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
Updating values of pandas dataframe on condition
提问by WoodChopper
I'm trying to update the pandas data frame by logical condition but, it fails with below error,
我正在尝试通过逻辑条件更新 Pandas 数据框,但失败并显示以下错误,
df[df.b <= 0]['b'] = 0
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
How do I get this working?
我如何让这个工作?
Data:
数据:
df = pd.DataFrame({'a': np.random.randn(4), 'b': np.random.randn(4)})
a b
0 1.462028 -1.337630
1 0.206193 -1.060710
2 -0.464847 -1.881426
3 0.290627 0.650805
I am learning pandas. In R, syntax is like below,
我正在学习Pandas。在 R 中,语法如下所示,
df[df$b <= 0]$b <- 0
回答by shanmuga
Use
用
df.loc[df.b <= 0, 'b']= 0
For efficiency pandas just creates a references from the previous DataFrame instead of creating new DataFrame every time a filter is applied.
Thus when you assign a value to DataFrame it needs tobe updated in the source DataFrame (not just the current slice of it). This is what is refered in the warning
为了提高效率,pandas 只是从以前的 DataFrame 创建一个引用,而不是在每次应用过滤器时创建新的 DataFrame。
因此,当您为 DataFrame 分配一个值时,它需要在源 DataFrame 中更新(不仅仅是它的当前切片)。这是警告中提到的内容
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
试图在来自 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替
To avoid this .locsyntax is used.
为了避免使用这种.loc语法。
For more information on DataFrame indexing
有关DataFrame 索引的更多信息
回答by Hackaholic
Try this:
尝试这个:
>>> df.ix[df['b']<=0] = 0
>>> df
a b
0 0.000000 0.000000
1 0.000000 0.000000
2 0.212535 0.491969
3 0.000000 0.000000
Note:Since v0.20 ixhas been deprecated. Use locor ilocinstead.
注意:由于 v0.20ix已被弃用。使用loc或iloc代替。
回答by Avinash Thombre
Follow below pattern for updating the value -
按照以下模式更新值 -
food_reviews_df.loc[food_reviews_df.Score <= 3, 'Score'] = 0
food_reviews_df.loc[food_reviews_df.Score >= 4, 'Score'] = 1

