【发布时间】:2013-12-28 14:38:19
【问题描述】:
我想根据我的 pandas 数据框中的第一个索引值进行条件替换。如果我有一个数据框,例如:
from pandas import *
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples, names=['first','second'])
data = DataFrame(randn(8,2),index=index,columns=['c1','c2'])
我认为我应该能够通过以下方式替换列中的值:
data.ix['bar']['c1'] = -999
但这会返回原始数据帧,保持不变。谁能解释这应该怎么做以及为什么我目前的方法不起作用?
【问题讨论】:
-
您可能更喜欢使用 NaN 来处理缺失数据。
-
@AndyHayden - 确实如此,我可能会使用 NaN。我只是在这里选择了-999作为示例。
标签: python-2.7 pandas multi-index