根据另一列、Python、Pandas 中的值删除一列的重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45059713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:59:00  来源:igfitidea点击:

Drop duplicates of one column based on value in another column, Python, Pandas

pythonpandasdataframeduplicatesconditional-statements

提问by Ahmed

I have a dataframe like this:

我有一个这样的数据框:

Date                PlumeO      Distance
2014-08-13 13:48:00  754.447905 5.844577 
2014-08-13 13:48:00  754.447905 6.888653
2014-08-13 13:48:00  754.447905 6.938860
2014-08-13 13:48:00  754.447905 6.977284
2014-08-13 13:48:00  754.447905 6.946430 
2014-08-13 13:48:00  754.447905 6.345506
2014-08-13 13:48:00  754.447905 6.133567
2014-08-13 13:48:00  754.447905 5.846046 
2014-08-13 16:59:00  754.447905 6.345506 
2014-08-13 16:59:00  754.447905 6.694847 
2014-08-13 16:59:00  754.447905 5.846046 
2014-08-13 16:59:00  754.447905 6.977284 
2014-08-13 16:59:00  754.447905 6.938860 
2014-08-13 16:59:00  754.447905 5.844577 
2014-08-13 16:59:00  754.447905 6.888653 
2014-08-13 16:59:00  754.447905 6.133567 
2014-08-13 16:59:00  754.447905 6.946430

I'm trying to keep the date with the smallest distance, so drop the duplicates dates and keep the with the smallest distance.

我试图保持距离最小的日期,所以删除重复的日期并保持距离最小的日期。

Is there a way to achieve this in pandas' df.drop_duplicatesor am I stuck using if statements to find the smallest distance?

有没有办法在Pandas中实现这一点,df.drop_duplicates还是我坚持使用 if 语句来找到最小距离?

回答by ayhan

Sort by distances and drop by dates:

按距离排序并按日期排序:

df.sort_values('Distance').drop_duplicates(subset='Date', keep='first')
Out: 
                   Date      PlumeO  Distance
0   2014-08-13 13:48:00  754.447905  5.844577
13  2014-08-13 16:59:00  754.447905  5.844577

回答by piRSquared

The advantage of these approaches is that it does not require a sort.

这些方法的优点是不需要排序。

Option 1
You can identify the index values for the minimum values with idxminand you can use it within a groupby. Use these results to slice your dataframe.

选项 1
您可以使用 标识最小值的索引值,idxmin并且可以在groupby. 使用这些结果来切片您的数据框。

df.loc[df.groupby('Date').Distance.idxmin()]

                   Date      PlumeO  Distance
0   2014-08-13 13:48:00  754.447905  5.844577
13  2014-08-13 16:59:00  754.447905  5.844577


Option 2
You can use pd.DataFrame.nsmallestto return the rows associated with the smallest distance.

选项 2
您可以使用pd.DataFrame.nsmallest返回与最小距离关联的行。

df.groupby('Date', group_keys=False).apply(
    pd.DataFrame.nsmallest, n=1, columns='Distance'
)

                   Date      PlumeO  Distance
0   2014-08-13 13:48:00  754.447905  5.844577
13  2014-08-13 16:59:00  754.447905  5.844577

回答by Zach O

I would say sort the data first and then drop the duplicate dates:

我会说先对数据进行排序,然后删除重复的日期:

stripped_data = df.sort_values('distance').drop_duplicates('date', keep='first')