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 delete just some rows in a dataframe according with some fields condition in Python?

I need to keep just the first row of a dataframe for each group of values in a ordered column.

I need to transform this (first column is ordered by name):

a = [[1,'a'],[1,'c'],[1,'b'],[2,'c'],[2,'b'],[2,'a'],[3,'d']]

into this (just the first row for each value type in the first ordered column):

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

a = [[1,'a'],[2,'c'],[3,'d']]

>Solution :

You can even get your desire output by convert to DF and by keeping the first value of each unique values of first column. lastly, converting back to list.

Code:

import pandas as pd

a = [[1,'a'],[1,'c'],[1,'b'],[2,'c'],[2,'b'],[2,'a'],[3,'d']]
a = pd.DataFrame(a).drop_duplicates(0, keep='first').values.tolist()
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