简体   繁体   English

如何使用 Altair 将 % 添加到 Pandas pivot 表中

[英]How to add % into Pandas pivot table using Altair

I am working on a survey and the data looks like this:我正在进行一项调查,数据如下所示:

ID    Q1    Q2    Q3    Gender    Age    Dept
001   Y      N    Y      F         22     IT
002   N      Y    Y      M         35     HR
003   Y      N    N      F         20     IT
004   Y      N    Y      M         54     OPRE
005   Y      N    Y      M         42     OPRE

So I created a pivot table like this:所以我创建了一个 pivot 表,如下所示:

Q1    #Respondents      %Res
Y        4               80
N        1               20

If I would like to slice it by Gender, then it should be like:如果我想按性别切片,那么它应该是:

Q1      #Res        %Rep
       M    F      M    F
Y      2    2      50   50
N      1    0      100   0

And if I want this to be applied to all the questions, I'd like to use Altiar which enables me to choose the question so that I don't need to execute the codes all the time.如果我想将此应用于所有问题,我想使用 Altiar,它使我能够选择问题,这样我就不需要一直执行代码。 So far, I only know how to create simple table by:到目前为止,我只知道如何通过以下方式创建简单表:

Q1 = pd.pivot_table(df,values = ['ID'], 
                index = ["Q1"], 
                aggfunc ={'ID': 'count', })
Q1['%Respondents'] = (Q1['ID']/Q1['ID'].sum())*100
Q1

I don't know how to break it by gender and apply Altair.我不知道如何按性别打破它并应用 Altair。 Please let me know if you could help!如果你能帮忙,请告诉我! Thanks!谢谢!

To break by gender.按性别划分。 Just utilized it as index, and then unstack it只是将其用作索引,然后将其拆开

Q1 = pd.pivot_table(df,values = ['ID'], 
                index = ["Q1","Gender"], 
                aggfunc ={'ID': 'count', }).unstack(level = 0)

Unfurtunataly cant help you with you graph Unfurtunataly 无法帮助您绘制图表

IIUC, you can pivot_table then add the result of division as a new foo column %Respondents IIUC,您可以pivot_table然后将除法结果添加为新的 foo 列%Respondents

out = df.pivot_table(index='Q1', columns=['Gender'], values=['ID'], aggfunc='count', fill_value=0)
out = (out.join(out[['ID']].div(out['ID'].sum(axis=1).values, axis=0)
                .mul(100)
                .rename(columns={'ID':'%Respondents'})))
print(out)

       ID    %Respondents
Gender  F  M            F      M
Q1
N       0  1          0.0  100.0
Y       2  2         50.0   50.0

To keep the ball rolling, here how you could add % into altair charts.为了让球继续滚动,这里你可以如何将 % 添加到 altair 图表中。

  • Step 1. Convert wide dataframe to a long one with melt.步骤 1. 将宽 dataframe 转换为带熔体的长款。 I made a simple bar chart with counts for Q1 - without percentage % yet.我制作了一个简单的条形图,其中包含 Q1 的计数 - 还没有百分比。
df=pd.DataFrame({'ID':[1, 2, 3, 4, 5],
'Q1':['Y', 'N', 'Y', 'Y', 'Y'],
'Q2':['N', 'Y', 'N', 'N', 'N'],
'Q3':['Y', 'Y', 'N', 'Y', 'Y'],
'Gender':['F', 'M', 'F', 'M', 'M'],
'Age':[22, 35, 20, 54, 42],
'Dept':['IT', 'HR', 'IT', 'OPRE', 'OPRE']}).melt(id_vars=['ID', 'Gender', "Age", "Dept"])

alt.Chart(df.query('variable=="Q1"'), title="Q1").mark_bar().encode(
    x=alt.X('value:N', title="", axis=alt.Axis(labelAngle=0)),
    y=alt.Y('count()', axis=alt.Axis(tickMinStep=1)),
    color="Gender",
    column=alt.Column("Gender", title="")
).properties(width=100, height =150)

计数图

  • Step 2: One of possible scenarios - how to calculate %:第 2 步:一种可能的情况 - 如何计算 %:
qs=df.variable.unique().tolist()
charts=[]

for q in qs:
    chart=alt.Chart(df.loc[df['variable']==q], title=q).transform_joinaggregate(
        total='count(*)'
        ).transform_calculate(
        pct='1 / datum.total').mark_bar().encode(
            y=alt.Y('value:N', title=""),
            x=alt.X('sum(pct):Q', axis=alt.Axis(format='%'), title=""),
            tooltip=[alt.Tooltip('sum(pct):Q',format=".1%")],
            color="Gender"
        ).properties(width=200)
    charts.append(chart)

alt.hconcat(*charts).configure_scale(
        bandPaddingInner=0.03
    )

具有百分比拆分的图表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM