python - Counting unique values in a pandas grouped object -
i have table in pandas/python , doing following:
grouped_data = df_comments_cols['article_id'].groupby(df_comments_cols['user_id'])
now count number of articles per user following:
ct_grouped_data = grouped_data.count()
the above counts number of article ids per user. however, there multiple of same article ids per user (in sense user has interacted article more once) , wish count unique article ids per user - there quick way this?
thanks in advance.
i think might looking nunique
, can call on groupby
objects so:
in [63]: df = dataframe({'a': randn(1000, 1)}) in [64]: df['user_id'] = randint(100, 1000, size=len(df)) in [65]: df['article_id'] = randint(100, size=len(df)) in [66]: gb = df.article_id.groupby(df.user_id) in [67]: gb.nunique() out[67]: user_id 100 2 101 1 102 1 104 2 105 1 106 2 107 1 110 1 111 4 112 2 113 1 114 2 115 1 116 1 118 1 ... 976 3 980 1 982 1 983 1 986 1 987 1 988 1 989 2 990 1 993 1 994 2 996 1 997 1 998 1 999 1 length: 617, dtype: int64
Comments
Post a Comment