Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas group the rows in a dataframe based on specific column value

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
like image 790
Mahamutha M Avatar asked Mar 19 '19 12:03

Mahamutha M


People also ask

How do I select rows from a DataFrame based on multiple column values?

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.


2 Answers

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
like image 194
jezrael Avatar answered Oct 04 '22 15:10

jezrael


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
like image 42
Vaishali Avatar answered Oct 04 '22 14:10

Vaishali