【发布时间】:2020-09-11 09:59:31
【问题描述】:
我有 2 个数据帧,我想从 badges 数据帧中删除其值大于 test_df 数据帧中相应 UserId's date 字段的行。问题是两个数据帧都有多个UserId 的值,并且两个数据帧的总行数不相等。我使用的代码只有在两个数据帧的行数相等时才有效,否则会产生错误。
部分徽章
UserId | Date
101 | 2009-09-01 15:17:50.660
101 | 2009-09-01 15:17:50.660
101 | 2009-09-02 15:17:50.660
102 | 2009-09-03 15:17:50.660
103 | 2013-09-30 15:17:50.660
103 | 2013-09-30 15:17:50.660
104 | 2009-09-30 15:17:50.660
test_df 的一部分
UserId | date
101 | 2010-09-01 15:17:50.660
101 | 2010-09-02 15:17:50.660
101 | 2010-09-03 15:17:50.660
102 | 2010-09-30 15:17:50.660
103 | 2010-09-30 15:17:50.660
103 | 2010-09-30 15:17:50.660
代码
badges = badges[(badges.Date < test_df.date)]
错误
ValueError: Can only compare identically-labeled Series objects
所需输出
badges
UserId | Date
101 | 2009-09-01 15:17:50.660
101 | 2009-09-01 15:17:50.660
101 | 2009-09-02 15:17:50.660
102 | 2009-09-03 15:17:50.660
104 | 2009-09-30 15:17:50.660
【问题讨论】:
标签: python python-3.x pandas dataframe datetime