简体   繁体   English

Pandas数据帧索引异常?

[英]Pandas dataframe indexing anomaly?

The Python code below first creates a multi-indexed pandas dataframe then attempts to change one of its elements. 下面的Python代码首先创建一个多索引的pandas数据帧,然后尝试更改其中一个元素。 The element in question is printed before and after the change to verify that the change worked. 在更改之前和之后打印有问题的元素,以验证更改是否有效。 The problem is that it does not work. 问题是它不起作用。 Please have a look at this code and let me know what the problem is. 请看一下这段代码,让我知道问题所在。

import numpy as np
import pandas as pd
arrays = [['Apple','Apple','Banana','Banana','Cherry','Cherry'],
         ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.zeros([3, 6]), index=['A', 'B', 'C'], columns=index)
df.insert(0, 'Insert', [1,2,3]) # the absence of this line makes the problem disappear
print df['Apple']['one']['A'] # this line correctly prints 0
df['Apple']['one']['A'] = 15
print df['Apple']['one']['A'] # this line again prints 0 when we should get 15 now

You need to do the following: 您需要执行以下操作:

df.loc['A', ('Apple', 'one')] = 15

It is not an anomaly, you are doing 'chained assignment' which basically only changes a copy of the underlying data. 这不是一个异常现象,你正在进行“链式分配”,它基本上只会更改底层数据的副本。 Someone will be able to tell you more accurately what is going on, but to index properly use .loc or .ix. 有人能够更准确地告诉你发生了什么,但要正确使用.loc或.ix。

See this answer: 看到这个答案:

How to deal with SettingWithCopyWarning in Pandas? 如何在Pandas中处理SettingWithCopyWarning?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM