pandas - Split Python Dataframe into multiple Dataframes (where chosen rows are the same) -
i split 1 dataframe n dataframes based on columns x , z same (as eachother column value).
for example, input:
df = name x y z other 0 1 1 1 1 1 b 1 1 2 2 2 c 1 2 1 3 3 d 1 2 2 4 4 e 1 1 1 5 5 f 2 1 2 6 6 g 2 2 1 7 7 h 2 2 2 8 8 2 1 1 9 9 j 2 1 2 0
would have output:
df_group_0 = name x y z other 0 1 1 1 1 2 c 1 2 1 3 4 e 1 1 1 5 df_group_1 = name x y z other 1 b 1 1 2 2 3 d 1 2 2 4 df_group_2 = name x y z other 6 g 2 2 1 7 8 2 1 1 9 df_group_3 = name x y z other 7 h 2 2 2 8 9 j 2 1 2 0
is possible?
groupby
generates iterator of tuples first element group id, if iterate through groupers , extract second element each tuple, can list of data frames each having unique group:
grouper = [g[1] g in df.groupby(['x', 'z'])] grouper[0] name x y z other 0 1 1 1 1 2 c 1 2 1 3 4 e 1 1 1 5 grouper[1] name x y z other 1 b 1 1 2 2 3 d 1 2 2 4 grouper[2] name x y z other 6 g 2 2 1 7 8 2 1 1 9 grouper[3] name x y z other 5 f 2 1 2 6 7 h 2 2 2 8 9 j 2 1 2 0
Comments
Post a Comment