【发布时间】:2014-01-26 15:18:46
【问题描述】:
我有两个关于 Python Pandas 数据帧的索引相关问题。
import pandas as pd
import numpy as np
df = pd.DataFrame({'id' : range(1,9),
'B' : ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'amount' : np.random.randn(8)})
df = df.ix[df.B != 'three'] # remove where B = three
df.index
>> Int64Index([0, 1, 2, 4, 6, 7], dtype=int64) # the original index is preserved.
1)我不明白为什么我修改数据框后索引没有自动更新。有没有办法在修改数据框时自动更新索引?如果没有,最有效的手动方式是什么?
2) 我希望能够将df 的第5 个元素的B 列设置为“三”。但是df.iloc[5]['B'] = 'three' 不会那样做。我检查了manual,但它没有介绍如何更改按位置访问的特定单元格值。
如果我按行名访问,我可以这样做:df.loc[5,'B'] = 'three',但我不知道索引访问等效项是什么。
【问题讨论】:
标签: python indexing pandas dataframe