[英]Correct way to set value in multi-index Pandas Dataframe
I'm trying to set a value in a multi-index dataframe. 我正在尝试在多索引数据框中设置一个值。 I am using .loc but I am still getting the SettingsWithCopyWarning.
我正在使用.loc,但我仍然得到了SettingsWithCopyWarning。 I have read the documentation but I'm not sure how I should be doing this.
我已经阅读了文档,但我不确定我应该怎么做。 What am I doing wrong?
我究竟做错了什么?
"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 “SettingWithCopyWarning:尝试在DataFrame的切片副本上设置一个值。尝试使用.loc [row_indexer,col_indexer] = value
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj[item] = s" 请参阅文档中的警告: http ://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self.obj [item] = s“
arrays = [np.array(['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D']),
np.array(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'])]
df = pd.DataFrame(np.random.randn(6, 8), columns=arrays)
mask = df.loc[:,('A','one')].ge(0)
df[mask].loc[:,('B')] = "A"
The SettingWithCopyWarning occurs because you are doing .loc[]
on a subset of the DataFrame. 发生SettingWithCopyWarning是因为您在DataFrame的子集上执行
.loc[]
。 Use .loc[]
to select the subset directly and you won't get this error. 使用
.loc[]
直接选择子集,您将不会收到此错误。
arrays = [np.array(['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D']),
np.array(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'])]
df = pd.DataFrame(np.random.randn(6, 8), columns=arrays)
mask = df.loc[:,('A','one')].ge(0)
df.loc[mask,('B')] = "A"
You can also simplify the line where you calculate the mask, since you don't need .loc[]
for this. 您还可以简化计算掩码的行,因为您不需要
.loc[]
。
mask = df[('A','one')].ge(0) # or, df[('A','one')] >= 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.