scott martin
scott martin

Reputation: 1293

Pandas Filtering row based on value using column index

I am trying to filter a Dataframe based on value in a column

cust_id, prod_type
101, prod_A
102, prod_A
102, prod_B
103, prod_F
103, prod_A
104, prod_D

I am trying to filter based on column index as below:

df.loc[df.columns[1].eq('prod_A')]

It throws an error AttributeError: 'str' object has no attribute 'eq'

Upvotes: 0

Views: 60

Answers (1)

jezrael
jezrael

Reputation: 863031

You can select second column by positions by DataFrame.iloc:

df.loc[df.iloc[:, 1].eq('prod_A')]

Your solution should be changed with df[], because df.columns[1] return column name:

df.loc[df[df.columns[1]].eq('prod_A')]

Upvotes: 3

Related Questions