python pandas:如何避免链接分配 [英] python pandas: how to avoid chained assignment
问题描述
我有一个熊猫数据框,其中有两列:x和value. 我想查找x == 10的所有行,并为所有这些行设置值= 1,000.我尝试了下面的代码,但得到警告
I have a pandas dataframe with two columns: x and value. I want to find all the rows where x == 10, and for all these rows set value = 1,000. I tried the code below but I get the warning that
A value is trying to be set on a copy of a slice from a DataFrame.
我知道我可以通过使用.loc或.ix来避免这种情况,但是我首先需要找到满足x == 10的条件的所有行的位置或索引.还有更直接的方法吗?
I understand I can avoid this by using .loc or .ix, but I would first need to find the location or the indices of all the rows which meet my condition of x ==10. Is there a more direct way?
谢谢!
import numpy as np
import pandas as pd
df=pd.DataFrame()
df['x']=np.arange(10,14)
df['value']=np.arange(200,204)
print df
df[ df['x']== 10 ]['value'] = 1000 # this doesn't work
print df
推荐答案
您应该使用loc
来确保您正在使用视图,在您的示例中,以下内容将起作用并且不会发出警告:
You should use loc
to ensure you're working on a view, on your example the following will work and not raise a warning:
df.loc[df['x'] == 10, 'value'] = 1000
所以一般形式是:
df.loc[<mask or index label values>, <optional column>] = < new scalar value or array like>
docs 突出显示了错误,并且有简介,的功能文档稀疏,可以随时提交改进.
The docs highlights the errors and there is the intro, granted some of the function docs are sparse, feel free to submit improvements.
这篇关于python pandas:如何避免链接分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!