使用Numpy与Pandas进行简单数据处理
1 | from pandas import DataFrame, Series |
显示奖牌数据:
1 | olympic_medal_counts_df = DataFrame( |
output>>
计算至少获得一枚金牌的国家所获得铜牌的平均数: 1
2avg_bronze_at_least_one_gold = numpy.mean(df[df.gold > 0].bronze)
print avg_bronze_at_least_one_gold
output>>4.2380952381
计算金牌、银牌与铜牌的平均数: 1
2avg_medal_count=numpy.mean(df[['gold','silver','bronze']])
print avg_medal_count
output>>
如果收获一枚金牌得4分,一枚银牌得2分,一枚铜牌得1分,计算所有国家的总分数
1 | df['points'] = df[['gold','silver','bronze']].dot([4,2,1]) |
output>>