Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (50.2k points)

I'm trying to select a subset of a subset of a dataframe, selecting only some columns, and filtering on the rows.

df.loc[df.a.isin(['Apple', 'Pear', 'Mango']), ['a', 'b', 'f', 'g']]

However, I'm getting the error:

Passing list-likes to .loc or [] with any missing label will raise

KeyError in the future, you can use .reindex() as an alternative.

What 's the correct way to slice and filter now?

1 Answer

0 votes
by (107k points)

This is a change introduced in v0.21.1,

For example,

df

     A    B  C

0  7.0  NaN  8

1  3.0  3.0  5

2  8.0  1.0  7

3  NaN  0.0  3

4  8.0  2.0  7

Try some kind of slicing as you're executing-

df.loc[df.A.gt(6), ['A', 'C']]

     A  C

0  7.0  8

2  8.0  7

4  8.0  7

No problem. Now, try substituting C with a non-existent column label -

df.loc[df.A.gt(6), ['A', 'D']]

FutureWarning: Passing list-likes to .loc or [] with any missing label will raise

KeyError in the future, you can make use of .reindex().

     A   D

0  7.0 NaN

2  8.0 NaN

4  8.0 NaN

So, in your illustration, the error is because of the column labels you pass to loc. Take another look at them.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...