Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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_Crosstab - Fatal编程技术网

python交叉表:使用多个列显示百分比时出错

python交叉表:使用多个列显示百分比时出错,python,pandas,crosstab,Python,Pandas,Crosstab,我正在尝试创建一个包含多个列的交叉表。它是使用以下代码正确创建的: crt= pd.crosstab(index=df.hc_est, columns=[df.year_iq,df.group], margins=True) year_iq 2017 2018 All cir_grup inter prim inter prim hc_est IA 0

我正在尝试创建一个包含多个列的交叉表。它是使用以下代码正确创建的:

crt= pd.crosstab(index=df.hc_est, columns=[df.year_iq,df.group], margins=True)

year_iq         2017           2018       All
cir_grup    inter   prim    inter   prim    
hc_est                  
IA              0   10          1   11     22
IC              0    4          0    0     4
IC1             0    0          0    1     1
....
IVA             4    1          2    0     7
IVB             1    0          1    0     2
All            13   20         12   19     64
我想在每列中添加一个百分比列(包括“全部”列)。为此,我尝试使用百分比创建相同的交叉表,然后在两个交叉表之间进行连接

但设置normalize参数时显示错误:

crt= pd.crosstab(index=df.hc_est, columns=[df.year_iq,df.group], margins=True, normalize=True)
TypeError: Expected tuple, got str

crt= pd.crosstab(index=df.hc_est, columns=[df.year_iq,df.group], margins=True, normalize='columns')
ValueError: Length of new names must be 1, got 2
“all”选项也给出了错误:“TypeError:Expected tuple,got str "

为什么会失败?是否有其他方法可以根据每列的总数向所有列添加百分比

dtypes: 
hc_est                object
year_iq               Int64
group                 object