python-3.x - 有没有办法使用 groupby() 将缺失值显示为 df 中的一行?
问题描述
我有 3 个 dfs,需要按客户类型汇总,并按每个地点的办公室访问次数计算。
目标是在一张 excel 表中输出这 3 个 dfs,并使每个最终输出表具有完全相同的行数(所有客户类型都以相同的顺序)。
但是,由于并非所有 df 都具有所有客户类型,有没有办法获得所需的输出?下面是代码(我简化了数据):
d1 = {'customer type':['walk in', 'online app', 'phone app', 'referral'], 'office visit':
['location1','location1','location1','location1']}
df1=pd.DataFrame(data=d1)
d2 = {'customer type': ['walk in', 'online app', 'phone app'], 'office visit':
['location1','location1','location1']}
df2=pd.DataFrame(data=d2)
d3={'customer type':['walk in','referral'],'office visit':['location1','location1']}
df3=pd.DataFrame(data=d3)
def info(df):
df=df.groupby(['customer type','office visit'],sort=True).size().unstack().fillna(0)
return df
df1=info(df1)
df2=info(df2)
df3=info(df3)
with pd.ExcelWriter(r'C:\users\JZ\patient.xlsx',engine='xlsxwriter') as writer:
df1.to_excel(writer,sheet_name='patient',startrow=0,startcol=0)
df2.to_excel(writer,sheet_name='patient',startrow=0, startcol=5)
df3.to_excel(writer,sheet_name='patient',startrow=0, startcol=10)
所需的输出将是所有 3 个 dfs 将有 4 行(每行显示每种客户类型 - 在线应用程序、电话应用程序、推荐和走进),无论他们是否拥有所有客户类型。对于 df2,推荐为 0。对于 df3,在线应用程序和电话应用程序均为 0。
任何帮助和建议将不胜感激。
解决方案
推荐阅读
- calculator - hp prime 计算器 RPN 模式 x^y 函数错误
- powershell - 无法完全正确地获取 PowerShell 管道语法
- scala - 一个动作如何使用类型来传达它没有主体?
- react-native - 反应原生如何在单个元素上使用多个动画
- python - 单击提交按钮后如何清除或刷新 OptionMenu
- firebase - Android 上的 Firebase 电话身份验证
- unit-testing - 如何在file.pl中模拟一个func而不是一个模块
- ms-access - 如何检查两个表中是否存在值
- server - 我的 shadowsocks 无法在 virmach 的服务器上运行的可能原因是什么?
- java - 解码 html 响应