我有一个DataFrame,我想从其中选择一个单元格。我可以通过行索引和列标签选择一个单元格,但是当我过滤数据帧时,同样的选择不起作用。
print("Title:",df.loc[1,'title']) # Has no error
mobiles = df.loc[df['cat3']=='mobile-phones']
print("Title:",mobiles.loc[1,'title']) # Has error对于最后一次打印,我得到了以下错误:
KeyError: 'the label [1] is not in the [index]'发布于 2019-03-18 23:09:18
当您将移动电话指定为:
mobiles = df.loc[df['cat3']=='mobile-phones']df['cat3']=='mobile-phones'在索引处满足条件的可能性不是1。
使用:
mobiles = df.loc[df['cat3']=='mobile-phones'].reset_index(drop=True)或者您可以使用.iloc[]来过滤第一个索引(它看不到标签名称)
https://stackoverflow.com/questions/55224307
复制相似问题