2

I have a dataframe wit more than 1000 rows and around 20 columns like this:

   UserID    C1    C2     C3  ...
    100      0    0.34   0.45
    100      2    0.5    0.45
    104      0.2   0.2   0.8 
    107      1.2   2.3   0.5
    107      5    0.8   0.9
    107      3    0.4   0.4
   ...

So I need to divide this data-frame into several data-frames based on the USerID that I can do the other process on each data-frame.

Psidom
  • 209,562
  • 33
  • 339
  • 356
RZA KHK
  • 35
  • 4
  • There are lots of related questions: https://stackoverflow.com/search?q=split+pandas+dataframe, this: https://stackoverflow.com/questions/23691133/split-pandas-dataframe-based-on-groupby maybe what you're after. – EdChum Jul 06 '17 at 14:40
  • Yes, I saw them but I could not find any case that can solve the problem. Since I need a list that contains multiple data frames. – RZA KHK Jul 06 '17 at 15:23

1 Answers1

2

If you want a dataframe per UserID in a list of dataframes you can use the follow code:

list_df = []

for n,g in df.groupby('UserID'):
    list_df.append(g)

For dictionary:

dict_df = dict(tuple(df.groupby('UserID')))
Scott Boston
  • 147,308
  • 15
  • 139
  • 187