如何使用Altair将%添加到熊猫枢轴表中
我正在研究调查,并且数据看起来像这样:
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
所以我创建了一个枢轴表:
Q1 #Respondents %Res
Y 4 80
N 1 20
如果我想通过性别切片,那么它应该是这样的:
Q1 #Res %Rep
M F M F
Y 2 2 50 50
N 1 0 100 0
如果我希望将其应用于所有问题,我想使用Altiar,使我能够选择这个问题,以便我不需要一直执行代码。 到目前为止,我只知道如何创建简单的表:
Q1 = pd.pivot_table(df,values = ['ID'],
index = ["Q1"],
aggfunc ={'ID': 'count', })
Q1['%Respondents'] = (Q1['ID']/Q1['ID'].sum())*100
Q1
我不知道如何通过性别打破它并应用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:
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.
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.
Please let me know if you could help! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iiuc,您可以
pivot_table
,然后将Division的结果添加为新的foo列%受访者
IIUC, you can
pivot_table
then add the result of division as a new foo column%Respondents
通过性别打破。只是将其用作索引,然后取消
堆放的张开,无法帮助您图形
To break by gender. Just utilized it as index, and then unstack it
Unfurtunataly cant help you with you graph
为了保持球的滚动,在这里如何将%添加到Altair图表中。
我制作了一个简单的条形图,其中Q1计数 - 尚无百分比%。
To keep the ball rolling, here how you could add % into altair charts.
I made a simple bar chart with counts for Q1 - without percentage % yet.