Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to drop specific pandas rows by value

Is there any way that I can drop the value if its index = column index.

I mean, this is my toy dataframe

d = {'Non': [1, 2,4,5,2,7], 'Schzerando': [3, 4,8,4,7,7], 'cc': [1,2,0.75,0.25,0.3,1]}
df = pd.DataFrame(data=d)
df

Then I just want to keep the row which df["cc"] == 1 and 2, like this
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Toy dataframe to try.

>Solution :

You can filter out the rows by converting the cc column to int type then filter by applying mask.

df['cc'] = df['cc'].astype('Int64')
df = df[df['cc'] == 1 | df['cc'] == 2 | df['cc'] == 3]

or you can declare a list with all the values you want to filter for then use pandas isin

f_list = [1,2,3]
df[df['cc'].isin(f_list)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading