这是用 Pandas 编写的脚本,我必须使用标准库重写它:
import pandas as pd
import sys
if __name__ == '__main__':
if len(sys.argv) != 1 :
print('usage: python by_continent.py')
sys.exit(1)
gap = pd.read_csv('gapminder.tsv', sep='\t')
means = gap.groupby('continent').mean()
parts = means[['lifeExp', 'gdpPercap']]
print( parts )
输入看起来像:
country continent year lifeExp pop gdpPercap
Zambia Africa 2002 39.193 10595811 1071.613938
Zambia Africa 2007 42.384 11746035 1271.211593
Zimbabwe Africa 1952 48.451 3080907 406.8841148
Zimbabwe Africa 1957 50.469 3646340 518.7642681
Zimbabwe Africa 1962 52.358 4277736 527.2721818
Zimbabwe Africa 1967 53.995 4995432 569.7950712
Zimbabwe Africa 1972 55.635 5861135 799.3621758
Zimbabwe Africa 1977 57.674 6642107 685.5876821
Zimbabwe Africa 1982 60.363 7636524 788.8550411
Zimbabwe Africa 1987 62.351 9216418 706.1573059
Zimbabwe Africa 1992 60.377 10704340 693.4207856
Zimbabwe Africa 1997 46.809 11404948 792.4499603
Zimbabwe Africa 2002 39.989 11926563 672.0386227
Zimbabwe Africa 2007 43.487 12311143 469.7092981
Argentina Americas 1952 62.485 17876956 5911.315053
Argentina Americas 1957 64.399 19610538 6856.856212
Argentina Americas 1962 65.142 21283783 7133.166023
Argentina Americas 1967 65.634 22934225 8052.953021
Argentina Americas 1972 67.065 24779799 9443.038526
Argentina Americas 1977 68.481 26983828 10079.02674
这是输出应该是什么:
lifeExp gdpPercap
continent
Africa 48.865330 2193.754578
Americas 64.658737 7136.110356
Asia 60.064903 7902.150428
Europe 71.903686 14469.475533
Oceania 74.326208 18621.609223
翻过高山走不出你
PIPIONE
catspeake
相关分类