简体   繁体   English

根据其他列值从熊猫 dataframe 中删除重复项

[英]Drop duplicates from a panda dataframe based on other column values

Dataframe which I am using is as below:我正在使用的 Dataframe 如下:

Name    NoOfTrans   Avg_pass_time    Cons.Error            RunCounts
Jan     0                            Failed:abcd           4
Jan                                                        4
Jan                                                        4
Jan                                                        4
May     2                            Failed:abcFailed:cde  5
May                                                        5
May                  1200                                  5
May                  1200                                  5
May                                                        5

I need to remove the duplicate from "Name", "Avg_pass_time" and "RunCounts" columns group by the "Name" column so that the output is as below:我需要从按“名称”列分组的“名称”、“Avg_pass_time”和“RunCounts”列中删除重复项,以便 output 如下所示:

Name    NoOfTrans   Avg_pass_time    Cons.Error            RunCounts
Jan     0                            Failed:abcd           4
May     2           1200             Failed:abcFailed:cde  5

Any guide will be usefull任何指南都会有用

You can select a subset of rows that will be used to drop the duplicates:您可以 select 将用于删除重复项的行子集:

df = df.drop_duplicates(subset=['Name','Avg_pass_time','RunCounts'])

Untested but this should work.未经测试,但这应该有效。

If per groups are only empty strings or duplicated values use:如果每组只有空字符串或重复值,请使用:

df = df.replace('',np.nan).groupby('Name', as_index=False).first().fillna('')

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

相关问题 如何根据 DataFrame Python Pandas 中其他 2 列中的值删除一列中的重复项? - How to drop duplicates in one column based on values in 2 other columns in DataFrame in Python Pandas? 通过添加来自其他列的值在 Panda 数据框中创建新列 - Make new column in Panda dataframe by adding values from other columns 根据其他列的值创建熊猫列 - Creating panda column based off of values from other columns 根据匹配的列值与其他数据框的组合删除行熊猫 - Drop rows pandas based on combination of matched column values with other dataframe 如何根据熊猫数据框中的其他列进行组合 - How to make combination based on other column in Panda dataframe 根据另一列的重复项删除一列的重复项,将另一列重复项保留在 pandas - drop duplicates of one column based on duplicates of another column keeping the other column duplicates in pandas 在Panda Dataframe列中添加值 - Add values in column of Panda Dataframe 根据其他列值删除重复项(Python) - Dropping duplicates based on other column values (Python) 如何根据列的值(列的名称不同)从 pandas dataframe 中删除重复的列? - How to drop duplicates columns from a pandas dataframe, based on columns' values (columns don't have the same name)? Pandas dataframe 删除基于另一列值的重复项 - Pandas dataframe drop duplicates based in another column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM