微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

KeyError:“[Index(['5', '22', '25', '12',..],\n dtype='object', length=610)] 都不在 [columns] 中”

如何解决KeyError:“[Index(['5', '22', '25', '12',..],\n dtype='object', length=610)] 都不在 [columns] 中”

试图剥离某个部分但抛出错误“列中没有索引”

数据集:

Here's an ss of the dataset

代码片段:

df4 = df[df["rating"].notna()]
df4  = df4[df4["rating"].str.strip("%").astype(int)]
df4

错误

KeyError: "None of [Int64Index([ 36,97,95,96,86,89,92,\n            ...\n            100,99,100,82,80,97],\n           dtype='int64',length=335)] are in the [columns]"
df5 = df[df["Experience"].str.strip("years experience")]
df5

错误

KeyError: "None of [Index(['5','22','25','12','33','13','15','5',\n       ...\n       '20','28','26','11','19','14','40','41'],\n      dtype='object',length=610)] are in the [columns]"

解决方法

你不能做这样的操作;
df4 = df4[df4["Rating"].str.strip("%").astype(int)] 尝试以合适的方式使用 apply。例如;
df4 = df4["Rating"].apply(lambda x: x.strip("%")).astype(int)
还有,
您的第一行 df[df["Rating"].notna()] 之所以有效,是因为 df["Rating"].notna() 返回布尔系列,而外部 df[] 对该系列进行操作,从而保留为该系列提供 True 的记录。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。