Pandas 根据特定的列值对数据框中的行进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55240674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:21:44  来源:igfitidea点击:

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

pythonpandasgrouping

提问by Mahamutha M

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

现在我需要根据列值“gw_mac”和“mac”对数据框进行分组,我应该得到以下三个不同的组

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

采纳答案by jezrael

If need different groups by columns loop by groupbyobject:

如果需要按列循环按groupby对象进行不同的组:

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

回答by Vaishali

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