我一直在处理一个包含用户ID、日期时间对象和其他信息的DataFrame,像下面这个例子:
User_ID;Latitude;Longitude;Datetime
222583401;41.4020375;2.1478710;2014-07-06 20:49:20
287280509;41.3671346;2.0793115;2013-01-30 09:25:47
329757763;41.5453577;2.1175164;2012-09-25 08:40:59
189757330;41.5844998;2.5621569;2013-10-01 11:55:20
624921653;41.5931846;2.3030671;2013-07-09 20:12:20
414673119;41.5550136;2.0965829;2014-02-24 20:15:30
414673119;41.5550136;2.0975829;2014-02-24 20:16:30
414673119;41.5550136;2.0985829;2014-02-24 20:17:30
我已将用户分组:
g = df.groupby(['User_ID','Datetime'])
然后检查是否存在单个DataTime对象:
df = df.groupby('User_ID')['Datetime'].apply(lambda g: len(g)>1)
我获得了以下布尔类型的数据框:
User_ID
189757330 False
222583401 False
287280509 False
329757763 False
414673119 True
624921653 False
Name: Datetime, dtype: bool
这对于我只想保留值为True的User_ID来说是可以的。现在,我想仅保留与True值相关联的User_ID值,并使用pandas.to_csv
将它们写入新的DataFrame中。预期的DataFrame将仅包含具有多个DateTime对象的User_ID:
User_ID;Latitude;Longitude;Datetime
414673119;41.5550136;2.0965829;2014-02-24 20:15:30
414673119;41.5550136;2.0975829;2014-02-24 20:16:30
414673119;41.5550136;2.0985829;2014-02-24 20:17:30
我如何获得每个用户ID的布尔值?感谢您的帮助。