根据 Pandas 0.16 中的条件更新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30432714/
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-13 23:23:35  来源:igfitidea点击:

updating columns based on conditions in pandas 0.16

pythonpandas

提问by desmond

I am trying to update a column based on condition of another column

我正在尝试根据另一列的条件更新一列

df=pd.DataFrame(np.random.randn(6,4),columns=list('abcd'))
df[df.b>0].d=1

why doesnt this work? without the condition it works.

为什么这不起作用?没有它的工作条件。

回答by Marius

When I do this with pandas v0.16.1, I get a warning telling me what's happening:

当我使用 pandas v0.16.1 执行此操作时,我收到一条警告,告诉我发生了什么:

df=pd.DataFrame(np.random.randn(6,4),columns=list('abcd'))
df[df.b>0].d=1
/home/me/.local/lib/python2.7/site-packages/pandas/core/generic.py:1974: SettingWithCopyWarning: 
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

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

df[df.b > 0]creates a copy of those rows of the dataframe that is no longer linked to the original dataframe. Following the suggestions in the warning, if I do:

df[df.b > 0]创建不再链接到原始数据帧的数据帧的那些行的副本。按照警告中的建议,如果我这样做:

df.loc[df.b > 0, 'd'] = 1

I get the desired results:

我得到了想要的结果:

df
Out[10]: 
          a         b         c         d
0 -0.127010  0.252527 -0.857680  1.000000
1  0.348888  0.780728 -0.710778  1.000000
2  0.840746 -0.456552  0.414482 -1.326191
3  0.864530  0.365728 -0.540530  1.000000
4  1.954639 -0.919998 -0.446927  1.949182
5 -0.928344 -0.145271  0.089434 -0.569934