Search code examples
pythonpandasdataframejupyter-notebookanalytics

How can i group pandas dataframe based on two colunms?


I have a pandas dataframe object in this way:

enter image description here

I want to create multiple dataframes from it grouped on user and alt_user columns. Basically, each newly created dataframe should have unique combination of user and alt_user.

Output:

enter image description here

enter image description here

enter image description here

enter image description here

So far I have used dataframe1.groupby(['user', 'alt_user']) But this does not create multiple dataframes.


Solution

  • dataframe1.groupby(['user', 'alt_user']) is not designed to create multiple dataframes, in fact it will not even create one without an aggregation. To do what you want, you need to start with a list of all unique pairs, which could be gotten in the following way:

    unique_pairs = set(zip(df.User, df.Alt_user))
    

    Then the list of dataframes would be

    dataframes = [dataframe1.loc[np.logical_and(dataframe1.User==user, dataframe1.Alt_user==alt_user), :] for user, alt_user in unique_pairs]