-1

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

数据集:

这是数据集的 ss

代码片段:

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

错误:

KeyError: "None of [Int64Index([ 36,  97,  97,  95,  96,  86,  89,  95,  92,  95,\n            ...\n            100,  92,  99, 100,  97, 100,  82,  97,  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', '12', '13',\n       ...\n       '20', '28', '26', '11', '13', '19', '15', '14', '40', '41'],\n      dtype='object', length=610)] are in the [columns]"
4

1 回答 1

1

你不能做这样的操作;
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为系列提供的记录。

于 2021-02-05T08:09:25.597 回答