【问题标题】:how to get group of data with maximum rows count pandas如何获取具有最大行数的数据组 pandas
【发布时间】:2021-03-20 22:00:56
【问题描述】:

对于我的数据框,我按天应用函数分组数据并计算行数,我想获得最大行数的组

在这里我得到每个组中的行数:

df1=pd.DataFrame({'x1':[5,5,4,6],'x2':[5,5,6,7],
                         'time':['11/7/2019','11/7/2019',
                                 '11/7/2019','12/7/2019']}) 
xx = (pd.to_datetime(df1['time'])
       .dt.floor('d')
       .value_counts()
       .rename_axis('date')
       .reset_index(name='count'))

输出目标:

 x1  x2       timep
0   5   5  11/7/2019
1   5   5  11/7/2019
2   4   6  11/7/2019

【问题讨论】:

    标签: python-3.x pandas numpy pandas-groupby


    【解决方案1】:

    使用groupby().transform():

    days= pd.to_datetime(df1['time']).dt.floor('d')
    s = df1.groupby(days)['time'].transform('size')
    
    df1[s == s.max()]
    

    输出:

       x1  x2       time
    0   5   5  11/7/2019
    1   5   5  11/7/2019
    2   4   6  11/7/2019
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多