Python 数据帧设置值失败
我一直在遵循文档,但它不断抛出此错误Python 数据帧设置值失败,python,pandas,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'
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__'
与
loc
不同,set\u value
是一种方法,因此需要调用它
但是df.set_值(['ab_3','n_1','1'])
也不会达到您期望的效果。
请参阅its。使用了错误类型的括号,您希望
()
而不是[]
:
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
如果我的回答解决了你的问题,那么你可以接受