如何通过按其他多列分组来查找列中值的百分比?

How to find percentage of a value in a column by grouping by other multiple columns?

我想查找列中具有特定值的行的百分比。并找出其他两列的每种组合的百分比。

这里有一个例子 df:

data =[['North Shields','UK','Y'],['North Shields','Foreign','N']]
df = pd.DataFrame(data, columns = ['Port','Type','Shellfish Licence licence (Y/N)']
df 

我尝试了以下但出现关键错误,可能是因为我无法以这种方式对两列进行 grouby。

port_shel_df = landing_fish_merge['Shellfish Licence licence 
(Y/N)'].eq('Y').groupby(port_merge_lic_df['Port','Type]).mean().reset_index(name='Shellfish 
license 
percentage')
port_shel_df = port_shel_df.set_index('Port')
port_shel_df[:1]

创建辅助列并按其聚合:

df = (landing_fish_merge.assign(new= landing_fish_merge['Shellfish Licence licence (Y/N)'].eq('Y'))
                        .groupby(['Port','Type'])['new'].mean()
                        .reset_index(name='Shellfish license percentage'))