首页 > 其他分享 >46.通过字典和Series对象进行分组统计

46.通过字典和Series对象进行分组统计

时间:2022-11-20 17:36:31浏览次数:36  
标签:set 46 Series 华东地区 df pd 字典 display 出库

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#通过字典进行分组统计

#通过字典进行分组统计
import pandas as pd 
pd.set_option ( 'display.unicode.east_asian_width', True) 
pd.set_option ('display.max_columns',500) 
pd.set_option ('display.width',1000) 
df=pd.read_excel('JD手机销售数据.xlsx') 
#print (df.head()) 
df=df.set_index('商品名称')   #设置商品名称为行索引 
dict1={'北京出库量':'华北地区',
       '上海出库量':'华东地区', 
       '广州出库量':'华南地区', 
       '天津出库量':'华北地区',
       '苏州出库量':'华东地区',
       '沈阳出库量':'东北地区',
       '杭州出库量':'华东地区',
      }
df1=df.groupby(dict1, axis=1).sum() 
print (df1)
 东北地区  华东地区  华北地区  华南地区
商品名称                                        
荣耀1          329       983       651       326
oppo1           70       449       522       196
Alpple1         71       452       917       328
oppo2           72       455      1050       329
oppo3           70       468       920        67
Alpple2         72       474      1054        69
荣耀2           74       480       106        89
Redmi1          76       486       109        90
Redmi2          78       492       112        91
oppo4           80       498       115        92

##通过Series对象进行分组统计

#通过Series对象进行分组统计
import pandas as pd 
pd.set_option ( 'display.unicode.east_asian_width', True) 
pd.set_option ('display.max_columns',500) 
pd.set_option ('display.width',1000) 
df=pd.read_excel('JD手机销售数据.xlsx') 
#print (df.head()) 
df=df.set_index('商品名称')   #设置商品名称为行索引 
dict1={'北京出库量':'华北地区',
       '上海出库量':'华东地区', 
       '广州出库量':'华南地区', 
       '天津出库量':'华北地区',
       '苏州出库量':'华东地区',
       '沈阳出库量':'东北地区',
       '杭州出库量':'华东地区',
      }
s=pd.Series(dict1)   #构建Series对象
df1=df.groupby(s, axis=1).sum() 
print(df1)
东北地区  华东地区  华北地区  华南地区
商品名称                                        
荣耀1          329       983       651       326
oppo1           70       449       522       196
Alpple1         71       452       917       328
oppo2           72       455      1050       329
oppo3           70       468       920        67
Alpple2         72       474      1054        69
荣耀2           74       480       106        89
Redmi1          76       486       109        90
Redmi2          78       492       112        91
oppo4           80       498       115        92

 

标签:set,46,Series,华东地区,df,pd,字典,display,出库
From: https://www.cnblogs.com/988MQ/p/16908994.html

相关文章