0

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'

1 Answer 1

3

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')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.