Home > Back-end >  Pandas select with iloc
Pandas select with iloc

Time:01-09

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 its 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).

CodePudding user response:

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]
  •  Tags:  
  • Related