我有一个数据框df:
first_seen last_seen uri
0 2015-05-11 23:08:46 2015-05-11 23:08:50 http://11i-ssaintandder.com/
1 2015-05-11 23:08:46 2015-05-11 23:08:46 http://11i-ssaintandder.com/
2 2015-05-02 18:27:10 2015-06-06 03:52:03 http://goo.gl/NMqjd1
3 2015-05-02 18:27:10 2015-06-08 08:44:53 http://goo.gl/NMqjd1
我想删除具有相同“ first_seen”,“ uri”的行,并仅保留具有最新last_seen的行。
这是expected
数据集的示例:
first_seen last_seen uri
0 2015-05-11 23:08:46 2015-05-11 23:08:50 http://11i-ssaintandder.com/
3 2015-05-02 18:27:10 2015-06-08 08:44:53 http://goo.gl/NMqjd1
有人知道谁在不编写for循环的情况下执行此操作吗?
调用drop_duplicates
并传递您要考虑重复匹配的列作为argssubset
并设置param take_last=True
:
In [295]:
df.drop_duplicates(subset=['first_seen','uri'], take_last=True)
Out[295]:
index first_seen last_seen uri
1 1 2015-05-11 23:08:46 2015-05-11 23:08:46 http://11i-ssaintandder.com/
3 3 2015-05-02 18:27:10 2015-06-08 08:44:53 http://goo.gl/NMqjd1
编辑
为了获取最新日期,您需要先在“ first_seen”和“ last_seen”上对df进行排序:
n [317]:
df = df.sort(columns=['first_seen','last_seen'], ascending=[0,1])
df.drop_duplicates(subset=['first_seen','uri'], take_last=True)
Out[317]:
index first_seen last_seen uri
0 0 2015-05-11 23:08:46 2015-05-11 23:08:50 http://11i-ssaintandder.com/
3 3 2015-05-02 18:27:10 2015-06-08 08:44:53 http://goo.gl/NMqjd1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句