为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Pandas DataFrame 计算百分比

使用 Pandas DataFrame 计算百分比

芜湖不芜 2023-07-18 15:21:18
这 5 个国家在历届奥运会上获得的奖牌总数中,他们各自获得的奖牌数所占的百分比是多少?我已使用 panda dataframe 将所有 excel 文件合并到一个文件中,但现在仍坚持查找百分比    Country      Gold     Silver    Bronze  Total0   USA          10       13         11      341   China        2        2          4       82   UK           1        0          1       23   Germany      12       16         8       364   Australia    2        0          0       20   USA          9        9          7       251   China        2        4          5       112   UK           0        1          0       13   Germany      11       12         6       294   Australia    1        0          1       20   USA          9        15         13      371   China        5        2          4       112   UK           1        0          0       13   Germany      10       13         7       304   Australia    2        1          0       3import pandas as pdimport numpy as npimport matplotlib.pyplot as pltdf= 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=it1=(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*100print(t3)print(t2)print(t4)这是如何得到答案的......现在我需要将其拉入情节我想把它放在馅饼中
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

假设您已将 DataFrame 创建为'df'. 然后您可以执行以下操作,首先分组,然后计算百分比。


df = df.groupby('Country').sum()

df['Gold_percent']   = (df['Gold']   / df['Gold'].sum()) * 100

df['Silver_percent'] = (df['Silver'] / df['Silver'].sum()) * 100

df['Bronze_percent'] = (df['Bronze'] / df['Bronze'].sum()) * 100

df['Total_percent']  = (df['Total']  / df['Total'].sum()) * 100

df.round(2)


print (df)

输出如下:


           Gold  Silver  Bronze  ...  Silver_percent  Bronze_percent  Total_percent

Country                          ...                                               

Australia     5       1       1  ...            1.14            1.49           3.02

China         9       8      13  ...            9.09           19.40          12.93

Germany      33      41      21  ...           46.59           31.34          40.95

UK            2       1       1  ...            1.14            1.49           1.72

USA          28      37      31  ...           42.05           46.27          41.38


查看完整回答
反对 回复 2023-07-18
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

我没有你所拥有的确切数据集。我正在用类似的数据集进行解释。尝试添加一列,其中包含跨行的奖牌总和。然后通过将所有行除以整列的总和来找到百分比。


我将此作为模型发布,请检查此


import pandas as pd


cars = {'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  RTOPrice

0     Honda Civic            21000      2200

1  Toyota Corolla            26000       250

2      Ford Focus            28000      2700

3         Audi A4            34000      3500





df['percentage']=(df.ExshowroomPrice +df.RTOPrice) * 100

/(df.ExshowroomPrice.sum() +df.RTOPrice.sum())

print(df)




            Brand  ExshowroomPrice  RTOPrice  percentage

0     Honda Civic            21000      2200   19.719507

1  Toyota Corolla            26000       250   22.311942

2      Ford Focus            28000      2700   26.094348

3         Audi A4            34000      3500   31.874203


查看完整回答
反对 回复 2023-07-18
  • 2 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信