[英]how to calculate percentage variation between two values?
I have this dataframe with the total population number by year.我有这个 dataframe 和每年的总人口数。
import pandas as pd
cases_df = pd.DataFrame(data=cases_list, columns=['Year', 'Population', 'Nation'])
cases_df.head(7)
Year Population Nation
0 2019 328239523 United States
1 2018 327167439 United States
2 2017 325719178 United States
3 2016 323127515 United States
4 2015 321418821 United States
5 2014 318857056 United States
6 2013 316128839 United States
I want to calculate how much the population has increased from the year 2013 to 2019 by calculating the percentage change between two values (2013 and 2019):我想通过计算两个值(2013 年和 2019 年)之间的百分比变化来计算从 2013 年到 2019 年人口增加了多少:
{[(328239523 - 316128839)/ 316128839] x 100 }
How can I do this?我怎样才能做到这一点? Thank you very much!!
非常感谢你!!
ps.附言。 some advice to remove index?
删除索引的一些建议?
0 1 2 3 4 5 6
i tried to to that我试过了
df1 = df.groupby(level='Population').pct_change()
print(df1)
but i get error because "Population" says that is not the name of Index但我得到错误,因为“人口”说那不是索引的名称
I would do it following way我会按照以下方式进行
import pandas as pd
df = pd.DataFrame({"year":[2015,2014,2013],"population":[321418821,318857056,316128839],"nation":["United States","United States","United States"]})
df = df.set_index("year")
df["percentage"] = df["population"] * 100 / df["population"][2013]
print(df)
output output
population nation percentage
year
2015 321418821 United States 101.673363
2014 318857056 United States 100.863008
2013 316128839 United States 100.000000
Note I used subset of data for brevity sake.请注意,为简洁起见,我使用了数据子集。 Using year as index allow easy access to population value in 2013, percentage is computed as (population) * 100 / (population for 2013).
使用年份作为索引可以轻松获取 2013 年的人口值,百分比计算为(人口)* 100 /(2013 年人口)。
How to remove the mentioned index:如何删除提到的索引:
df.set_index('Year',inplace=True)
Now Year will replace your numbered index.现在 Year 将替换您的编号索引。
Now现在
Use cases_df.describe()
or cases_df.attribute_name.describe()
使用
cases_df.describe()
或cases_df.attribute_name.describe()
This is more of a math question rather than a programming question.这更像是一个数学问题而不是编程问题。
Let's call this a percentage difference between two values since population can vary both ways (increase or decrease over time).我们将其称为两个值之间的百分比差异,因为人口可以双向变化(随时间增加或减少)。
Now, lets say that in 2013 we had 316128839
people and in 2019 we had 328239523
people:现在,假设 2013 年我们有
316128839
人,2019 年我们有328239523
人:
a = 316128839
b = 328239523
Before we go about calculating the percentage, we need to find the difference between the b
and a
:在我们 go 计算百分比之前,我们需要找出
b
和a
之间的差异:
diff = b - a
Now that we have that, we need to see what is the percentage of diff
of a
:现在我们有了,我们需要看看
a
的diff
百分比是多少:
perc = (diff / a) * 100
And there is your percentage variation between a
and b
还有你在
a
和b
之间的百分比变化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.