2017-03-29 6 views
0

私は以下のデータを持っています。パンダ - IDでグループ化し、重複を削除する

userid itemid timestamp 
    1  1  50 
    1  2  50 
    1  3  50 
    1  4  60 
    2  1  40 
    2  2  50 

タイムスタンプが重複しているユーザーを削除します。 上記の例では、timestamp = 50で複数の項目を表示しているため、userid = 1を削除します。 userid = 2は、アイテムが異なるタイムスタンプで表示されているため、削除しないでください。

誰でも手伝ってもらえますか?

答えて

1

オプション1
使用duplicated

dropid = df.loc[ 
    df.duplicated(subset=['userid', 'timestamp']), 
    'userid' 
].unique() 
df[~df.userid.isin(dropid)] 

オプション2
使用groupbyfilter

df.set_index(['userid', 'timestamp']).groupby(level=0).filter(
    lambda x: ~x.index.is_unique 
).reset_index() 
関連する問題