Pandas, for each unique value in one column, get unique values in another column

后端 未结 3 517
情话喂你
情话喂你 2020-12-25 08:01

I have a dataframe where each row contains various meta-data pertaining to a single Reddit comment (e.g. author, subreddit, comment text).

I want to do the following

3条回答
  •  一个人的身影
    2020-12-25 09:02

    Using groupby.agg() "aggrgeate" function:

    *

    DataFrameGroupBy.agg(arg, *args, **kwargs): aggregate using one or more operations over the specified axis. Function to use for aggregating the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply

    df = pd.DataFrame({'numbers': [1, 2, 3, 6, 9], 'colors': ['red', 'white', 'blue', 'red', 'white']}, columns=['numbers', 'colors'])
    


    df.groupby('colors', as_index=True).agg({'numbers' : {"unique" : lambda x: set(x),
                                                          "nunique" : lambda x : len(set(x))}})
    

提交回复
热议问题