Luca
Luca

Reputation: 10996

pandas filtering by a boolean series

I am having issue where I am trying to filter a dataframe rows where the corresponding entry in a pandas series object is true.

So, looking at the dataset, I have my input dataframe with size:

[37697 rows x 12 columns]

And then I have the corresponding filtering series as:

0        False
1        False
2        False
3        False
4        False
         ...  
37692    False
37693    False
37694    False
37695    False
37696    False
Name: _merge, Length: 37697, dtype: bool

So, they have the same length but when I do something like:

df[df.columns[_merge]]

I get:

boolean index did not match indexed array along dimension 0; dimension is 12 but corresponding boolean dimension is 37697

I tried using r.transpose() but that still gives the same error.

Upvotes: 1

Views: 537

Answers (1)

Valdi_Bo
Valdi_Bo

Reputation: 30971

df.columns returns a list of columns in your DataFrame.

Then you wrote [_merge], so you attempt to access columns using boolean indexing.

And since your column list has different length from the length of _merge, an exception has been raised.

Try boolean indexing, but on rows of your DataFrame:

df[_merge]

Upvotes: 2

Related Questions