Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - KeyError: "None of [Index(['5', '22', '25', '12',..], dtype='object', length=610)] are in the [columns]"

Trying to strip a certain part but throw an error "None of index in the columns"

Dataset :

Here's an ss of the dataset

Code snippets :

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

Error:

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

Error :

KeyError: "None of [Index(['5', '22', '25', '12', '33', '13', '15', '5', '12', '13',
       ...
       '20', '28', '26', '11', '13', '19', '15', '14', '40', '41'],
      dtype='object', length=610)] are in the [columns]"
question from:https://stackoverflow.com/questions/66059596/keyerror-none-of-index5-22-25-12-n-dtype-object-length-6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can not do operations like this;
df4 = df4[df4["Rating"].str.strip("%").astype(int)] try to use apply in suitable manner. for example;
df4 = df4["Rating"].apply(lambda x: x.strip("%")).astype(int)
And,
your first line df[df["Rating"].notna()] works because df["Rating"].notna() returns boolean series and outer df[] operate on a that series such a way that remain records that give True for the series.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...