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
You can select the Rows from Pandas DataFrame based on column values or based on multiple conditions either using DataFrame. loc[] attribute, DataFrame. query() or DataFrame. apply() method to use lambda function.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With