我有数据
id event_path
111 google.com
111 yandex.ru
111 vk.com
222 twitter.com
222 twitter.com
333 twitter.com
333 facebook.com欲望输出
id event_path
111 google.com
111 yandex.ru
111 vk.com
222 twitter.com
333 twitter.com
333 facebook.com我尝试使用shift to column
df.loc[(df.event_path != df.event_path.shift()) & \
(df.id == df.id.shift())]它会回报我
id event_path
111 google.com
111 yandex.ru
111 vk.com
222 twitter.com
333 facebook.com我怎么才能解决呢?
发布于 2017-11-29 14:58:35
您可以使用shift为consecutives值创建助手系列,添加列id并获取duplicated。最后一次boolean indexing滤除
df1=df[~df[['id']].join(df['event_path'].ne(df['event_path'].shift()).cumsum()).duplicated()]发布于 2017-11-16 10:37:50
使用pd.DataFrame.drop_duplicates
df.drop_duplicates()
id event_path
0 111 google.com
1 111 yandex.ru
2 111 vk.com
3 222 twitter.com
5 333 twitter.com
6 333 facebook.comIIUC: OP只希望在副本相邻时才删除。
df[df.eq(df.shift().bfill()).any(1)]
id event_path
0 111 google.com
1 111 yandex.ru
2 111 vk.com
4 222 twitter.com
5 333 twitter.com
6 333 facebook.comhttps://stackoverflow.com/questions/47327391
复制相似问题