首页 > 解决方案 > 有没有办法使用 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。

任何帮助和建议将不胜感激。

标签: python-3.xpandaspandas-groupbypandas.excelwriter

解决方案


推荐阅读