groupby

作者: dreampai | 来源:发表于2019-09-26 14:45 被阅读0次
    import pandas as pd
    import numpy as np
    
    
    # 创建一个 DataFrame 对象
    ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
             'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
             'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
             'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
             'Points':[876,789,863,673,741,812,756,7988,64,701,804,690]}
    df = pd.DataFrame(ipl_data)
    print (df)
    
    # 将数据拆分组
    print (df.groupby('Team'))
    # 查看分组
    print (df.groupby('Team').groups)
    # 多列分组
    print (df.groupby(['Team','Year']).groups)
    
    grouped = df.groupby('Year')
    # 迭代遍历分组
    for name,group in grouped:
        print (name)
        print (group)
    
    # 获取一个分组
    print('Get group 2014: ')
    print (grouped.get_group(2014))
    
    # 聚合
    print('Group agg: test mean ')
    print (grouped['Points'].agg(np.mean))
    # 多个聚合函数
    agg = grouped['Points'].agg([np.sum, np.mean, np.std])
    print (agg)
    # 过滤
    filter = df.groupby('Team').filter(lambda x: len(x) >= 3)
    print (filter)
    
    

    相关文章

      网友评论

          本文标题:groupby

          本文链接:https://www.haomeiwen.com/subject/slxnuctx.html