Kevin
Kevin

Reputation: 1223

Pandas select with iloc

I have a 1 dimetional DataFrame:

df:

   feat1   feat2   feat3  ...  featn
0   0.12   0.002    0.53        0.23

I want to filter all the columns that their value is bigger than 0.2:

df[df.iloc[0] > 0.2]

desired output:

    feat3  ...  featn
0    0.53        0.23

But I receive an error:

pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Upvotes: 0

Views: 289

Answers (1)

mozway
mozway

Reputation: 260410

When doing a boolean slicing, a list/Series matching the length of rows is expected, and this filters the rows.

Explicitly subset the columns:

df.loc[:, df.iloc[0] > 0.2]

Upvotes: 2

Related Questions