简体   繁体   English

如何根据 Python 中的某些字段条件仅删除数据框中的某些行?

[英]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):进入这个(只是第一个有序列中每个值类型的第一行):

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

You can even get your desire output by convert to DF and by keeping the first value of each unique values of first column.您甚至可以通过转换为 DF 并保留第一列的每个唯一值的第一个值来获得您想要的输出。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM