zoukankan      html  css  js  c++  java
  • 20-pandas聚合

    import numpy as np
    import pandas as pd
    #1.聚合一次
    df=pd.DataFrame({"age":[18,20,22,22,23,23],
                     "name":["A","B","C","D","E","F"],
                     "price1":[1000,900,800,700,600,600],
                     "price2":[10,9,8,7,6,6]})
    print(df)
    result1=df.groupby("age")["price1"]#按照age抓取price
    print(result1.groups)
    print(result1.sum())#总和
    print(result1.mean())#平均
    print(result1.max())
    print(result1.min())
    
    #2.聚合多次
    d1={'item':['萝卜','白菜','辣椒','冬瓜','萝卜','白菜','辣椒','冬瓜'],
        'color':['white','white','red','green','white','white','red','green'],
        'weight':[1,2,3,4,1,2,3,4],
        'price':[1,2,3,4,1,2,3,4]}
    df=pd.DataFrame(d1)
    result=df.groupby("color")["price"]
    print(result.groups)
    print(result.sum()["white"])
    print(result.sum()["red"])
    print(result.max()["white"])
    print(result.mean()["white"])
    
    result2=df.groupby("item")["price","weight"]
    print(result2.groups)
    print(result2.sum()["weight"]["萝卜"])
    print(result2.sum()["weight"].萝卜)
    
    sums=df.groupby("color").sum().add_prefix("avg__")
    print(sums)
    print(pd.merge(pd,sums,left_on="color",right_index=True))
    

      

  • 相关阅读:
    Python从文件中读取数据
    Python中类的继承
    Python中的类(2)
    Python中的类
    自动登陆抽屉(1)
    爬取汽车之家新闻
    Django简介
    web应用,http协议简介,web框架
    CentOS7安装python3.6
    MySQL之索引
  • 原文地址:https://www.cnblogs.com/wcyMiracle/p/12448064.html
Copyright © 2011-2022 走看看