基本上,如果我想在原始数据框中更改数据,我总是需要使用.loc[]
。但考虑以下情况
>>> import pandas as pd
>>> from scipy import random
>>> from numpy import arange
>>> T, N = 4, 5
>>> TIndex = arange(0, T)
>>> FIndex = arange(0, N)
>>> wp = pd.Panel(items=['A', 'w', 'l', 'a', 'x', 'X', 'd', 'profit'],
... major_axis=TIndex, minor_axis=FIndex)
>>> wp.loc['a', 0, 0] = 0
>>> df = wp.loc[0, 'a']
>>> df.loc[0, 'a'] = 'test'
>>> df.loc[0, 'a']
Out[379]: 'test'
>>> wp.loc['a', 0, 0]
Out[380]: 0
我错在哪里了?
同时,没有抛出SettingWithCopyWarning
。当我在数据框级别犯这些错误时,通常会出现这种情况。这非常令人恼火。
T
和N
是什么?最好提供一个自包含的例子。 - logcdf = wp.loc[0, 'a']
处抛出了 KeyError 错误。我这里没有 0 在你的项目中。 - TomAugspurger