使用 Pandas DataFrame 计算百分比

这 5 个国家在历届奥运会上获得的奖牌总数中,他们各自获得的奖牌数所占的百分比是多少?


我已使用 panda dataframe 将所有 excel 文件合并到一个文件中,但现在仍坚持查找百分比


    Country      Gold     Silver    Bronze  Total

0   USA          10       13         11      34

1   China        2        2          4       8

2   UK           1        0          1       2

3   Germany      12       16         8       36

4   Australia    2        0          0       2

0   USA          9        9          7       25

1   China        2        4          5       11

2   UK           0        1          0       1

3   Germany      11       12         6       29

4   Australia    1        0          1       2

0   USA          9        15         13      37

1   China        5        2          4       11

2   UK           1        0          0       1

3   Germany      10       13         7       30

4   Australia    2        1          0       3


import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

df= pd.DataFrame()

for f in ['E:\\olympics\\Olympics-2002.xlsx','E:\\olympics\\Olympics- 

2006.xlsx','E:\\olympics\\Olympics-2010.xlsx',

      'E:\\olympics\\Olympics-2014.xlsx','E:\\olympics\\Olympics- 

2018.xlsx']:

data = pd.read_excel(f,'Sheet1')

df = df.append(data)


df.to_excel("E:\\olympics\\combineddata.xlsx")

data = pd.read_excel("E:\\olympics\\combineddata.xlsx")

print(data)


final_Data={}

for i in data['Country']:

x=i

t1=(data[(data.Country==x)].Total).tolist()


print("Name of Country=",i, int(sum(t1)))

final_Data.update({i:int(sum(t1))})



t3=data.groupby('Country').Total.sum()

t2= df['Total'].sum()

t4= t3/t2*100

print(t3)

print(t2)

print(t4)

这是如何得到答案的......现在我需要将其拉入情节我想把它放在馅饼中


芜湖不芜
浏览 92回答 2
2回答

慕标琳琳

假设您已将 DataFrame 创建为'df'. 然后您可以执行以下操作,首先分组,然后计算百分比。df = df.groupby('Country').sum()df['Gold_percent']   = (df['Gold']   / df['Gold'].sum()) * 100df['Silver_percent'] = (df['Silver'] / df['Silver'].sum()) * 100df['Bronze_percent'] = (df['Bronze'] / df['Bronze'].sum()) * 100df['Total_percent']  = (df['Total']  / df['Total'].sum()) * 100df.round(2)print (df)输出如下:           Gold  Silver  Bronze  ...  Silver_percent  Bronze_percent  Total_percentCountry                          ...                                               Australia     5       1       1  ...            1.14            1.49           3.02China         9       8      13  ...            9.09           19.40          12.93Germany      33      41      21  ...           46.59           31.34          40.95UK            2       1       1  ...            1.14            1.49           1.72USA          28      37      31  ...           42.05           46.27          41.38

眼眸繁星

我没有你所拥有的确切数据集。我正在用类似的数据集进行解释。尝试添加一列,其中包含跨行的奖牌总和。然后通过将所有行除以整列的总和来找到百分比。我将此作为模型发布,请检查此import pandas as pdcars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],        'ExshowroomPrice': [21000,26000,28000,34000],'RTOPrice': [2200,250,2700,3500]}df = pd.DataFrame(cars, columns = ['Brand', 'ExshowroomPrice','RTOPrice'])            Brand  ExshowroomPrice  RTOPrice0     Honda Civic            21000      22001  Toyota Corolla            26000       2502      Ford Focus            28000      27003         Audi A4            34000      3500df['percentage']=(df.ExshowroomPrice +df.RTOPrice) * 100/(df.ExshowroomPrice.sum() +df.RTOPrice.sum())print(df)            Brand  ExshowroomPrice  RTOPrice  percentage0     Honda Civic            21000      2200   19.7195071  Toyota Corolla            26000       250   22.3119422      Ford Focus            28000      2700   26.0943483         Audi A4            34000      3500   31.874203
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python