0

In my Dataframe, I would like to choose only specific columns based on a certain condition from a particular column.

Here is my output of initial Data Frame using df.head()

I would like to find for column equals to 'B' and display it with selected columns.

My code:

df = pd.read_csv('cancer_data.csv')

#To display column diagnosis equals B
df[df['diagnosis'] == 'B']

#To display selected columns
df[['diagnosis','radius_mean','perimeter_mean','area_mean']]

How to merge the condition and display it with selected columns only.

Thanks

1
  • df[df['diagnosis'] == 'B'][['diagnosis','radius_mean','perimeter_mean','area_mean']]. Commented Apr 9, 2019 at 17:08

1 Answer 1

3

Use df.loc:

df.loc[df['diagnosis'] == 'B', ['diagnosis','radius_mean','perimeter_mean','area_mean']]
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.