Home > front end >  Split dataframe to sub dataframes and fill content according to the relevant dataframe? [duplicate]
Split dataframe to sub dataframes and fill content according to the relevant dataframe? [duplicate]

Time:10-06

I have the following toy dataframe:

x  value      id
A  5           1
B  DE          1
C  False       1
D  yes         1
E  -0.9442     1
A  66          2
D  yes         2
E  -0.9442     2
F  Kuku        2
Z  1           2

etc. for each id, we have some set of attributes. I want to "slice" them by attributes for each id (for example id 1 has A,B,C,D and E attributes and id 2 has A,D,E,F and Z attributes. How can I create such sub dataframes containing only A,B,C,D and E attributes, A,D,E,F and Z attributes and so on - like boxes of same attributes where I will store the request id's with the appropriate attributes?

What I am trying to do is to split the whole dataframe by id's and then extract the attributes of each. Is there any method to do this in a simpler manner?

CodePudding user response:

You can create a dict of .groupby() objects of x grouped by id, as follows:

x_df_dict = {a: b for a, b in df.groupby('id')['x']}

Then, you can access the sub-dataframes (more accurately sub-Series) of x by id, as follows:

print(x_df_dict[1])

0    A
1    B
2    C
3    D
4    E
Name: x, dtype: object
print(x_df_dict[2])

5    A
6    D
7    E
8    F
9    Z
Name: x, dtype: object

  • Related