利用Python进行数据分析_数据聚合与分组运算_数据聚合

GroupBy

按发行人汇总2021年截至目前债券实际发行规模的统计

from pandas import Series,DataFrame
import pandas as pd
import pymysql
db = pymysql.connect(host='127.0.0.1',
                    port =3306,
                    user = 'root',
                    password = 'root',
                   database = 'jydb',charset='GBK')
sql = """SELECT MainCode,BondNature,Issuer,PlanIssueSize,ActualIssueSize FROM Bond_IssueNew where IssueDateStart>='2021-01-01"""
df = pd.read_sql(sql,db)
grouped = df['ActualIssueSize'].groupby(df['Issuer'])#按Issuer进行分组,并计算ActualIssueSize的和
df1 = grouped()
df1.to_excel('2.xlsx')

执行结果:

利用Python进行数据分析_数据聚合与分组运算_数据聚合

 

 

 

 对分组进行迭代

 

from pandas import Series,DataFrame
import pandas as pd
import pymysql
db = pymysql.connect(host='127.0.0.1',
                    port =3306,
                    user = 'root',
                    password = 'root',
                   database = 'jydb',charset='GBK')
sql = """SELECT MainCode,BondNature,Issuer,PlanIssueSize,ActualIssueSize FROM Bond_IssueNew where IssueDateStart>='2021-01-01'"""
df = pd.read_sql(sql,db)
for (k1,k2),group in df.groupby(['BondNature','Issuer']):
    print(k1,k2)
    print(group)

 

执行结果:

利用Python进行数据分析_数据聚合与分组运算_数据聚合

 

上一篇:JAVA获取当前时间的常用方法


下一篇:pandas读取Excel测试数据