熊猫:df.iloc工作正常,但df.at在完全相同的单元格上出现错误?

时间:2019-12-30 03:56:26

标签: python pandas

我有这两行,第一行打印ADNER FRANCISCO,而第二行则给出错误,即使它们应该相同,对吗?

print(df.iloc[y]['CleanCopy'])
print(df.at[y,'CleanCopy'])

奇怪的是,当我尝试使用非常简单的数据集来重新测试该问题以进行测试时,它可以正常工作并显示“ ADNER FRANCISCO”:

x = pd.DataFrame([['ba',1],['b',2],['c',' ADNER FRANCISCO ']], columns = ['Item','Value'])
print(x.at[2,'Value'])

我无法理解完全相同的命令如何作用于一个数据集,而不适用于另一个数据集。谁能猜出为什么我会收到此错误?谢谢!

p.s:第一个数据没什么问题,因为df.iloc[y]['CleanCopy']也会打印'ADNER FRANCISCO'...所以这与df.at特别有关。

f

1 个答案:

答案 0 :(得分:1)

发生这种情况的原因是您的索引无法继续。(从0到n)

x.index=[999,1000,1001]
x.iloc[2]['Value'] # this work since it is located by the position 
' ADNER FRANCISCO '
x.at[2,'Value'] # this failed since it is located by the index value. In my example , the number 2 dose not included in the index value