使用 pandas 进行数据分析的百分比 python 代码
percentage python code for data analysis using pandas
我想编写 Python 代码来分析来自网页 (http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/202_EN.htm) 的 Python 2.7 的 m_tax
和 m_tan
的百分比。我已经有了数据框代码,但我无法编写百分比代码。有人可以帮我写代码吗?谢谢!
datum m_ta m_tax m_taxd m_tan m_tand
------- ----- ----- ---------- ----- ----------
1901-01 -4.7 5.0 1901-01-23 -12.2 1901-01-10
1901-02 -2.1 3.5 1901-02-06 -7.9 1901-02-15
1901-03 5.8 13.5 1901-03-20 0.6 1901-03-01
1901-04 11.6 18.2 1901-04-10 7.4 1901-04-23
1901-05 16.8 22.5 1901-05-31 12.2 1901-05-05
1901-06 21.0 24.8 1901-06-03 14.6 1901-06-17
1901-07 22.4 27.4 1901-07-30 16.9 1901-07-04
1901-08 20.7 25.9 1901-08-01 14.7 1901-08-29
您可以调用div
and pass the sum
列来添加%
列:
In [66]:
df['m_tax%'],df['m_tan%'] = df['m_tax'].div(df['m_tax'].sum()) * 100, df['m_tan'].div(df['m_tax'].sum()) * 100
df
Out[66]:
datum m_ta m_tax m_taxd m_tan m_tand m_tax% m_tan%
0 1901-01 -4.7 5.0 1901-01-23 -12.2 1901-01-10 3.551136 -8.664773
1 1901-02 -2.1 3.5 1901-02-06 -7.9 1901-02-15 2.485795 -5.610795
2 1901-03 5.8 13.5 1901-03-20 0.6 1901-03-01 9.588068 0.426136
3 1901-04 11.6 18.2 1901-04-10 7.4 1901-04-23 12.926136 5.255682
4 1901-05 16.8 22.5 1901-05-31 12.2 1901-05-05 15.980114 8.664773
5 1901-06 21.0 24.8 1901-06-03 14.6 1901-06-17 17.613636 10.369318
6 1901-07 22.4 27.4 1901-07-30 16.9 1901-07-04 19.460227 12.002841
7 1901-08 20.7 25.9 1901-08-01 14.7 1901-08-29 18.394886 10.440341
我想编写 Python 代码来分析来自网页 (http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/202_EN.htm) 的 Python 2.7 的 m_tax
和 m_tan
的百分比。我已经有了数据框代码,但我无法编写百分比代码。有人可以帮我写代码吗?谢谢!
datum m_ta m_tax m_taxd m_tan m_tand
------- ----- ----- ---------- ----- ----------
1901-01 -4.7 5.0 1901-01-23 -12.2 1901-01-10
1901-02 -2.1 3.5 1901-02-06 -7.9 1901-02-15
1901-03 5.8 13.5 1901-03-20 0.6 1901-03-01
1901-04 11.6 18.2 1901-04-10 7.4 1901-04-23
1901-05 16.8 22.5 1901-05-31 12.2 1901-05-05
1901-06 21.0 24.8 1901-06-03 14.6 1901-06-17
1901-07 22.4 27.4 1901-07-30 16.9 1901-07-04
1901-08 20.7 25.9 1901-08-01 14.7 1901-08-29
您可以调用div
and pass the sum
列来添加%
列:
In [66]:
df['m_tax%'],df['m_tan%'] = df['m_tax'].div(df['m_tax'].sum()) * 100, df['m_tan'].div(df['m_tax'].sum()) * 100
df
Out[66]:
datum m_ta m_tax m_taxd m_tan m_tand m_tax% m_tan%
0 1901-01 -4.7 5.0 1901-01-23 -12.2 1901-01-10 3.551136 -8.664773
1 1901-02 -2.1 3.5 1901-02-06 -7.9 1901-02-15 2.485795 -5.610795
2 1901-03 5.8 13.5 1901-03-20 0.6 1901-03-01 9.588068 0.426136
3 1901-04 11.6 18.2 1901-04-10 7.4 1901-04-23 12.926136 5.255682
4 1901-05 16.8 22.5 1901-05-31 12.2 1901-05-05 15.980114 8.664773
5 1901-06 21.0 24.8 1901-06-03 14.6 1901-06-17 17.613636 10.369318
6 1901-07 22.4 27.4 1901-07-30 16.9 1901-07-04 19.460227 12.002841
7 1901-08 20.7 25.9 1901-08-01 14.7 1901-08-29 18.394886 10.440341