I have the data frame as like below one,
Input DataFrame
gw_mac mac
0 ac233fc015f6 dce83f3bc820
1 ac233fc015f6 ac233f264a4c
2 ac233fc015f6 ac233f264a4c
3 ac233fc015f6 dce83f3bc820
4 ac233fc015f6 ac233f264a4c
5 ac233fc015f6 ac233f264a4c
6 ac233fc015f6 dce83f3bc820
7 ac233fc015f6 e464eecba5eb
Now I need to group the dataframe based on the column values "gw_mac" and "mac" and I should get the following three different groups
Expected Output
Group1
gw_mac mac
0 ac233fc015f6 dce83f3bc820
3 ac233fc015f6 dce83f3bc820
6 ac233fc015f6 dce83f3bc820
Group2
gw_mac mac
1 ac233fc015f6 ac233f264a4c
2 ac233fc015f6 ac233f264a4c
4 ac233fc015f6 ac233f264a4c
5 ac233fc015f6 ac233f264a4c
Group3
gw_mac mac
7 ac233fc015f6 e464eecba5eb
If need different groups by columns loop by groupby
object:
for i, g in df.groupby(['gw_mac','mac']):
print (g)
gw_mac mac
1 ac233fc015f6 ac233f264a4c
2 ac233fc015f6 ac233f264a4c
4 ac233fc015f6 ac233f264a4c
5 ac233fc015f6 ac233f264a4c
gw_mac mac
0 ac233fc015f6 dce83f3bc820
3 ac233fc015f6 dce83f3bc820
6 ac233fc015f6 dce83f3bc820
gw_mac mac
7 ac233fc015f6 e464eecba5eb
You can try this to create a dictionary of data frames with unique groups,
df['Group'] = df.groupby(['gw_mac', 'mac']).cumcount()
dfs = dict(tuple(df.groupby('Group')))
You can access a group using,
dfs[0]
gw_mac mac Group
0 ac233fc015f6 dce83f3bc820 0
1 ac233fc015f6 ac233f264a4c 0
7 ac233fc015f6 e464eecba5eb 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.