简体   繁体   English

如何计算列中每个值的百分比遵循 python pandas dataframe 中的每个类别

[英]How to calculate the percentage of each value in a column follow each category in python pandas dataframe

I'm having a dataframe and trying to get the output which shows percentage of each value in different category.我有一个 dataframe 并试图获得 output ,它显示了不同类别中每个值的百分比。 Can anyone help on how can I do it?谁能帮助我怎么做?

Raw data table:原始数据表:

Interface_Bin接口_Bin Product产品
1 1 ADL日常活动
1 1 ADL日常活动
22 22 ADL日常活动
97 97 ADL日常活动
1 1 JSL JSL
1 1 JSL JSL
97 97 JSL JSL
97 97 JSL JSL
22 22 JSL JSL

Expected outcome:预期结果:

Product产品 Bin(97)_count Bin(97)_count Total_interfacebin_count Total_interfacebin_count Bin_97_percentage_vs total count bin_97_percentage_vs 总计数
ADL日常活动 1 1 4 4 25% 25%
JSL JSL 2 2 5 5 40% 40%

Thanks alot.非常感谢。

first sort the data so you get a dictionary (or list) like: {"ADL":121,"JSL":218} probably with code like首先对数据进行排序,以便得到一个字典(或列表),例如: {"ADL":121,"JSL":218} 可能带有类似的代码

for element in table row:
  if dict.contains(element.key) //in this case ADL or JSL
    dict[element.key].value+=element.value
  else
    dict[element.key]=element.value

then go through and sum all the dictionary values to get the total sum, or sum them as you add to the dictionary in the above code finally get each percentage as (dict[key].value/sum)*100 + "%"然后 go 通过并求和所有字典值以获得总和,或者将它们加到上述代码中的字典中,最终得到每个百分比为 (dict[key].value/sum)*100 + "%"

Use crosstab with concat :crosstabconcat一起使用:

df = pd.crosstab(df['Product'], df['Interface_Bin'])

f1 = lambda x: f'Bin({x})_count'
f2 = lambda x: f'Bin({x})_percentage_vs total count'
s = df.sum(axis=1).rename('Total_interfacebin_count')

df2 = df.div(s, axis=0).rename(columns=f2).mul(100)
df = pd.concat([df.rename(columns=f1), s, df2], axis=1).sort_index(axis=1)
print (df)
         Bin(1)_count  Bin(1)_percentage_vs total count  Bin(22)_count  \
Product                                                                  
ADL                 2                              50.0              1   
JSL                 2                              40.0              1   

         Bin(22)_percentage_vs total count  Bin(97)_count  \
Product                                                     
ADL                                   25.0              1   
JSL                                   20.0              2   

         Bin(97)_percentage_vs total count  Total_interfacebin_count  
Product                                                               
ADL                                   25.0                         4  
JSL                                   40.0                         5  

暂无
暂无

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

相关问题 计算数据框中每个列值的订单百分比 - Calculate order percentage for each column value in dataframe 熊猫-如何为每个组计算列中的每个值等于或小于该值的百分比 - Pandas - How to calculate for each group ,for each value in a column what percentage of value is equal and less than that 用于计算pandas列中每个值的百分比的函数 - Function to calculate the percentage each value has in a pandas column 为 pandas dataframe 中的列中的每个值计算列表中每个元素的 perc - Calculate perc of each element in a list for each value in column in pandas dataframe 如何使用pandas数据框计算每个单元格的百分比并用结果(%)替换tha值? - how to calculate percentage for each cell and replace tha value with result(%) using pandas dataframe? 如何分组并计算熊猫每列中不丢失值的百分比? - how to groupby and calculate the percentage of non missing values in each column in pandas? 如何使用pandas groupby计算每列中的总数百分比 - How to use pandas groupby to calculate percentage of total in each column 如何按列值对python pandas数据帧进行十进制,然后对每个十进制求和? - How to decile python pandas dataframe by column value, and then sum each decile? 如何根据条件 pandas dataframe 计算每列特定值的出现次数及其百分比? - How to compute occurrencies of specific value and its percentage for each column based on condition pandas dataframe? 计算熊猫中多索引DataFrame每列的最小值 - Calculate minimum value for each column of multi-indexed DataFrame in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM