大数跨境

AI Python+Excel多表处理与分析系列

AI Python+Excel多表处理与分析系列 Power BI商业智能分析
2026-11-24
0
如下图,在订单表有如下图的明细数据源,在客户表有对应的性别字段
现在需要进行年份的性别销量分析
常用的做法是在Excel进行数据匹配,通过Vlookup完成对应的客户性别匹配,然后新增日期设计年份字段,最后在数据透视表进行数据透视分析,得到年份对应的性别的销量分析,具体步骤如下
步骤1:
步骤2:
步骤3:
将上述过程写到Python中,具体如下,在前面介绍了Python导入xlsx的数据源,同时介绍了如何将导入进来的日期字段进行格式转换,使其成为日期格式的字段,接下来新增年份列,使用了dt.year函数,这是前面介绍的内容
import  pandas as pdimport openpyxl from numpy.ma.core import dividepd.set_option('display.max_columns', 500)
import  pandas as pdimport openpyxlfrom numpy.ma.core import dividepd.set_option('display.max_columns', 500)
def year_gender():        pharma_sales = pd.read_excel("/Users/mac/Desktop/PBI资料包/销售产品数据源/pharma_sales.xlsx")        customers = pd.read_excel('/Users/mac/Desktop/PBI资料包/销售产品数据源/customers.xlsx')        order_date = pd.to_datetime(pharma_sales['order_date'])        pharma_sales["year"] = order_date.dt.year        pharma_sales_customers = pd.merge(pharma_sales, customers, how='left', on='customer_id')        gender_year = pharma_sales_customers.pivot_table(index=['gender'], columns=['year'], aggfunc='sum',values='quantity')        print(f"{gender_year}")if __name__ == '__main__':        year_gender()
本篇文章在这个基础上,完成多表匹配分析,这里使用merge函数,在merge函数参数中,通过将订单表与客户表传递进去,然后使用how参数进行left join连接,连接条件是customer_id字段,这样就能查询分析每一个订单对应的客户性别,如下
pharma_sales_customers = pd.merge(pharma_sales, customers, how='left', on='customer_id')    
最后在使用pivot_table函数,完成对应的年份性别对应的销量分析,如下
gender_year = pharma_sales_customers.pivot_table(index=['gender'], columns=['year'], aggfunc='sum',values='quantity')      print(f"{gender_year}")
结果如下图
与Excel数据分析的结果一致
上述思维逻辑在Power BI同样得到体现,在Power BI模型中,默认就是通过左连接关联了订单表与客户表,这样直接在前端可视化中将性别与对应的年份进行矩阵分析,一样得到上述的效果。

GPT+Gemini+Claude Code系列AI课程


Power BI+AI数据可视化分析产品+视频目录完整版,具体链接如下
GPT+Gemini+Claude Code系列AI课程
Power BI+AI Gemini Agent数据分析视频课
Excel建模+AI Skills高效制作PPT分析报告

2025PowerBI零基础可视化分析课程

PowerBI销售数据可视化分析产品5.0版

PowerBI供应链基础数据可视化产品4.0版

PowerBI渠道流向管理数据可视化分析产品3.0版

PowerBI人效与经销商分析可视化产品

Power BI上市财报可视化产品完整版

PowerBI预算分析可视化产品

Power BI应收账款可视化分析看板

PowerBI业财一体零基础实战应用

PowerBI上市财报可视化产品4.0版

PowerBI市场分析可视化产品1.0版全网首发

PowerBI上市公司估值可视化产品1.0版

Power BI电商数据分析产品(含PPT与视频版)

Power BI+SQL医药企全链路经营分析

医药经营分析会产品Power BI+PPT1.0版

SaaS+Power BI供应链渠道流向与合规分析产品

Excel从入门到进阶可视化分析看板课程

Excel销售数据可视化产品
Excel供应链数据可视化产品

【声明】内容源于网络
0
0
Power BI商业智能分析
站在Excel的肩膀上洞察数据商业价值。
内容 666
粉丝 0
Power BI商业智能分析 站在Excel的肩膀上洞察数据商业价值。
总阅读10.6k
粉丝0
内容666