【问题标题】:Automatically calculating percentage and store into variables自动计算百分比并存储到变量中
【发布时间】:2020-10-08 10:14:39
【问题描述】:

我目前正在进行人口统计项目。 我有来自 3 个不同国家的数据及其每个月的出生统计数据。

我的问题: 我想计算每个月出生的人的百分比并为每个国家绘制它。 (x = 月,y = 出生百分比) 因此,我想先计算百分比。 我想在所有月份的迭代中执行此操作,以改进我的代码。到目前为止:

EU = df2["European Union - 28 countries (2013-2020)"]
CZE = df2["Czechia"]
GER = df2["Germany including former GDR"]

EU_1 = EU[1] / EU[0] *100
EU_2 = EU[2] / EU[0] *100
etc.

每个月和 3 个国家/地区。

如何通过更改 Country [i] 自动计算所有值并分别存储每个值(函数、for 循环?)

非常感谢!

【问题讨论】:

    标签: python function for-loop variables automatic-storage


    【解决方案1】:

    你可以这样做:

    EU = df2["European Union - 28 countries (2013-2020)"]
    monthly_percentages = [num_born / EU[0] * 100 for num_born in EU[1:]]
    

    假设 EU 的第一个元素是当年的总出生人数,其余的是每月的出生人数。如果您想自动计算所有国家/地区,您可以遍历每个国家/地区并计算每个月的出生百分比,然后将其存储在某个地方。它看起来像:

    country_birth_percentages = []
    
    for country in countries:
        country_birth_percentages.append([num_born / country[0] * 100 for num_born in country[1:]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多