Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当数据透视表中的列有多个选项时,如何计算数据透视表中的行百分比-使用python_Python_Pandas_Pivot_Percentage - Fatal编程技术网

当数据透视表中的列有多个选项时,如何计算数据透视表中的行百分比-使用python

当数据透视表中的列有多个选项时,如何计算数据透视表中的行百分比-使用python,python,pandas,pivot,percentage,Python,Pandas,Pivot,Percentage,我试图创建一个透视表,在其中可以看到每个值的行百分比。 这是,假设我有索引='gender',其中gender为{Male,Female,Other},在列的一侧,我有一个对“你有孩子吗?”的响应,0表示否,1表示是 我想做一个表格,不仅计算每个性别有孩子和没有孩子的人数,而且计算每个价值观的百分比 我试过: `pivot1 = pd.pivot_table(df, index='gender', columns=['children'], aggfunc='size') pivot1.colu

我试图创建一个透视表,在其中可以看到每个值的行百分比。 这是,假设我有索引='gender',其中gender为{Male,Female,Other},在列的一侧,我有一个对“你有孩子吗?”的响应,0表示否,1表示是

我想做一个表格,不仅计算每个性别有孩子和没有孩子的人数,而且计算每个价值观的百分比

我试过:

`pivot1 = pd.pivot_table(df, index='gender', columns=['children'], aggfunc='size')
pivot1.columns = pivot1.columns.ravel()
pivot1['% of Total'] = (df.children / df.children.sum() * 100)
pivot1.loc['total', :] = pivot1.sum().values
pivot1`
但“儿童”列中的0和1并没有对该百分比进行归因:

额外:
还有一种方法可以使用多列数据透视表,但不使用多索引吗?

也许您想添加一个完整的示例,其中包含数据和预期结果。这使帮助更容易。同时,请看,这可能符合您的要求。也许您希望添加一个完整的示例,其中包含数据和预期结果。这使帮助更容易。同时看看,这可能符合你的要求。