简体   繁体   English

熊猫根据特定的列值将数据框中的行分组

[英]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 现在,我需要根据列值“ 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

If need different groups by columns loop by groupby object: 如果需要不同的组按列循环由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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据列值更改在 pandas dataframe 中选择所需的行分组 - Selecting the desired rows group by in pandas dataframe based on column value change 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 根据列值重复 pandas DataFrame 中的行 - Repeat rows in a pandas DataFrame based on column value 将Pandas数据帧分组一列,根据另一列删除行 - Group Pandas dataframe by one column, drop rows based on another column pandas 根据列中的重复值对 dataframe 中的行进行分组,并在 Uniqe ID 值之后重复所有行 - The pandas group the rows in the dataframe based on the repeated values in the column and repeat all after an Uniqe ID value 如何基于一组行在Pandas DataFrame中新建列 - How to create a new column in Pandas DataFrame based on a group of rows 根据对列中每个不同值具有特定条件的行计算 Pandas Dataframe 中的 perc - Calculate perc in Pandas Dataframe based on rows having a specific condition for each distinct value in column 熊猫:如何根据特定的行值将值应用于一组行? - Pandas: How to apply a value to a group of rows based on a specific row value? 根据一列过滤熊猫数据框:保留所有行(如果值是该列) - Filter pandas dataframe based on a column: keep all rows if a value is that column 如何根据特定列在 Pandas Dataframe 中向上移动行 - How to shift rows up in Pandas Dataframe based on specific column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM