简体   繁体   English

按每个唯一列值的最近日期过滤 Pandas 数据框

[英]Filter Pandas dataframe by most recent date for each unique column value

I have a dataframe:我有一个数据框:

      Col1          timestamp

1    x20034   2019-11-19 14:56:51
2    x20035   2019-11-20 12:16:22
3    x20035   2019-12-09 17:47:44
4    x20037   2019-12-08 17:47:44
5    x20037   2019-12-09 18:47:44
6    x20037   2019-12-10 22:47:40

And I need:我需要:

1    x20034   2019-11-19 14:56:51
2    x20035   2019-11-20 12:16:22
6    x20037   2019-12-10 22:47:40

I tried:我试过:

df[df['timestamp'] == df['timestamp'].max()]

Is this the best/only way?这是最好的/唯一的方法吗?

尝试transform

out = df[df['timestamp'] == df.groupby('Col1')['timestamp'].transform('max')]

暂无
暂无

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

相关问题 Pandas DataFrame - 如何在按另一列分组时获取每列的最新值 - Pandas DataFrame - How to get most recent value for each column when grouped by another column pandas dataframe函数返回日期最近的行,并且其中一列包含输入值,抛出错误 - pandas dataframe function to return rows where date is most recent and one of the column contains the input value, throwing error Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 在熊猫数据框中找到最近的日期 - find most recent date in pandas dataframe Python - 在 Pandas 数据框中获取最新日期 - Python - Get most recent date in a pandas dataframe 在 dataframe 列中查找第二个最近的日期 - Find second most recent date in a dataframe column Pandas:在 dataframe 的列中过滤每个唯一单元格值的日期字段 - Pandas: Filter datefield for each unique cell values in a column in dataframe 熊猫数据框,获取每行的最近日期 - Pandas dataframe, get recent date in each row 另一列的每个唯一值的30个最新数据点的平均值 - Mean of 30 most recent data points for each unique value of another column 用熊猫数据框中的最新日期和每月百分比变化值填充系列-python - Fill the series with value of most recent date and monthly percentage change in pandas dataframe - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM