--心有猛虎,细嗅蔷薇,学习编程成就更好的自己--

Python语言简洁生动,特别适合文科生学习入门IT世界,用几十行代码就能够做一个完整的爬虫脚本,开发效率杠杠的!短时间内即可解决工作和学习中碰到的棘手问题。(本人外语专业毕业,机缘巧合爱上编程,自学道路曲曲折折,痛并快乐!)在这里总结一下自学Python遇到的难点和重点,分享码过的代码和要点总结,希望能够给初学者一点启示和鼓励,同时愿意结交更多大神交流有助提升自己的水平。

平时在做数据处理和分析时经常使用pandas进行各种透视操作(pivot_table),而透视中最常见的是进行两个维度交叉查询,此时得到的透视结果集往往是二维表带多级标题,如何把二维表变换成为一维表呢?今天就针对这个问题展开展示和讲解,接下来看看实际案例吧:

原始数据如下:

该数据源主要包括日期,产品名称和销售收入,一般通过交叉透视查询可以得到不同产品在指定日期范围内的销售收入情况,但形成的结果往往是多维表并带多层标题,如何降维成一维表方便满足其他需求呢(例如生成热力图)?

下面来讲讲如何实现吧:

读取和查看数据基本情况:

添加必要的新字段方便后续处理:

通过pivot方法做交叉透视查询(产品和日期维度交叉)结果如下:

看到该表的产品上方是日期&星期标题,而日期&星期具体数据上方则是收入标题,如何消除日期&星期标题收入标题呢?这样得到的就是一维数据表了。

关键步骤1-重置该透视表索引:

可以看到产品来到了数据帧里面,同时一个新的索引出现了,该索引是关键哦!!!!方便后续拼接数据。

关键步骤2-抽取日期对应的数据内容,数据索引还是那个新索引:

注意:这里得到的还是DataFrame即数据帧,这个部分就是透视表中的核心数值内容,但缺少产品名称。

关键步骤3-抽取产品名数据,数据索引还是那个共同的新索引:

关键步骤4-通过concat方法横向拼接两个数据得到结果:

最后把产品名称改为索引列如下:

是不是感觉到索引列十分有用和神奇哇!!

该一维结果集方便阅读同时方便其他处理,例如数据可视化(热力图)如下:

总结处理的全部过程,写成一个自定义函数来完成这个需求:

代码汇总如下:

#import seaborn to draw heatmap import plotly.graph_objects as go import matplotlib.pyplot as plt import plotly_express as px import seaborn as sns import pandas as pd import numpy as np import datetime import os #Set work path def Set_Work_Path(x): try: os.chdir(x) #Set work file path route = os.getcwd() print(route) #print the changed path return route except Exception: print("No Result") work_path = r"E:DATA20211002" Set_Work_Path(work_path) #读取和检查数据 data = pd.read_excel("DATA-Heatmap.xlsx",header=0,sheet_name="DATA",engine="openpyxl") display(data.head()) display(data.dtypes) #构造新的字段方便后续分析 data["Weekday"] = data["Date"].dt.strftime("%a") data["Date&Weekday"] = data["Date"].astype(str) +"-"+ data["Weekday"].astype(str) display(data.head()) #通过pivot方法获取透视结果,纵轴显示产品而横轴显示日期和星期 pivot = pd.pivot_table(data,index=["Product"],columns=["Date&Weekday"],values=["Revenue"],aggfunc={"Revenue":np.sum},fill_value=0) display(pivot) #通过重置索引方便后续拼接数据 pivot.reset_index(level=0,inplace=True) display(pivot) #获取日期星期对应的数据和对应索引 target_content = pivot["Revenue"].astype(int) display(target_content) display(type(target_content)) #获取产品对应的数据和对应索引 target_column = pivot["Product"] display(target_column) display(type(target_column)) #把两个数据通过索引横向拼接在一起形成一维数据 target_data = pd.concat([target_column, target_content],axis=1) #concat two Dataframes with index display(target_data) #将产品重置为索引列 target_data.set_index("Product",inplace=True) display(target_data) #https://seaborn.pydata.org/tutorial/color_palettes.html#tools-for-choosing-color-palettes def Get_Heatmap(X): f, ax = plt.subplots(figsize=(20,5)) fig = sns.heatmap(X,annot=True,fmt="d",linewidths=0.5,cmap="RdBu_r") #Blues crest nowTime = datetime.datetime.now() Update_Time = nowTime.strftime('%Y-%m-%d-%H-%M') data_out_title = "Product-Revenue-Heatmap-"+Update_Time fig.set_title(label=data_out_title) fig.xaxis.set_ticks_position("top") return plt.show() Get_Heatmap(target_data) #定义一个降维函数 def Get_Pivot_To_df(X): try: pivot = pd.pivot_table(X,index=["Product"],columns=["Date&Weekday"],values=["Revenue"],aggfunc={"Revenue":np.sum},fill_value=0) pivot.reset_index(level=0,inplace=True) target_content = pivot["Revenue"].astype(int) target_column = pivot["Product"] target_data = pd.concat([target_column, target_content],axis=1) #concat two Dataframes with index target_data.set_index("Product",inplace=True) return target_data except Exception: pass target = Get_Pivot_To_df(data) display(target)

解决这个问题的关键就是重置索引后得到一个共同的新索引,新索引利用concat方法进行横向拼接,大家是不是也看明白了,如果有兴趣那就赶紧动手试一下哇!!!解决这个问题可能还有更简洁和优雅的办法,如果其他小伙伴恰好知道和熟悉别的途径,欢迎与本人分享和讨论。

END

我为人人,人人为我!!欢迎大家关注,点赞和转发!!!

~~人生不是赛场,梦想不容退场~~不断努力学习蜕变出一个更好的自己,不断分享学习路上的收获和感悟帮助他人成就自己!!!