RZA KHK
RZA KHK

Reputation: 35

Dividing a dataframe into several dataframes

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.

Upvotes: 2

Views: 110

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

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')))

Upvotes: 2

Related Questions