0

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.

Kush
  • 39
  • 8
  • It does create the groups, but these are all stored in the grouped object. What do you want to do with each individual group? As pandas allows you to apply the same operation to each group all at once. – Alex Jul 15 '21 at 09:29
  • 1
    Does this answer your question? [Splitting dataframe into multiple dataframes](https://stackoverflow.com/questions/19790790/splitting-dataframe-into-multiple-dataframes) – Anurag Dabas Jul 15 '21 at 09:50

1 Answers1

0

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]
Philip Egger
  • 326
  • 1
  • 11