pandas Dataframe设置值失败

时间:2016-08-04 10:58:48

标签: python pandas

我一直关注文档,但它继续抛出此错误

row_names = ["ab_" + str(x) for x in range(4)]
col_names = ["n_" + str(x) for x in range(5)]

df = pd.DataFrame(index=row_names, columns=col_names)
df = df.fillna(0) # with 0s rather than NaNs
# Load the images 
print df.loc['ab_3','n_1']
df.set_value['ab_3','n_1', '1']
print df

错误:

TypeError: 'instancemethod' object has no attribute '__getitem__'

2 个答案:

答案 0 :(得分:2)

使用错误类型的括号,您希望()不是[]

df.set_value('ab_3','n_1', '1')

      n_0  n_1  n_2  n_3  n_4
ab_0    0    0    0    0    0
ab_1    0    0    0    0    0
ab_2    0    0    0    0    0
ab_3    0    1    0    0    0

答案 1 :(得分:1)

loc不同,set_value是一种方法,因此需要调用它。

但是df.set_value(['ab_3','n_1', '1'])也不会做你期望的事情。 请参阅其docs

相关问题