>>> from pandas import Series, DataFrame >>> d = {'one': Series([1,2,3], index=['a','b','c']), ... 'two': Series([1,2,3,4], index=['a','b','c','d'])} >>> df = DataFrame(d) >>> df one two a 1.0 1 b 2.0 2 c 3.0 3 d NaN 4 >>> import numpy >>> df.apply(numpy.mean) one 2.0 two 2.5 dtype: float64 >>> df['one'].map(lambda x: x>= 1) a True b True c True d False Name: one, dtype: bool >>> df.applymap(lambda x: x>= 1) one two a True True b True True c True True d False True
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from pandas import DataFrame, Series import numpy def avg_bronze_medal_count(): countries = [ 'Russian Fed.' , 'Norway' , 'Canada' , 'United States' , 'Netherlands' , 'Germany' , 'Switzerland' , 'Belarus' , 'Austria' , 'France' , 'Poland' , 'China' , 'Korea' , 'Sweden' , 'Czech Republic' , 'Slovenia' , 'Japan' , 'Finland' , 'Great Britain' , 'Ukraine' , 'Slovakia' , 'Italy' , 'Latvia' , 'Australia' , 'Croatia' , 'Kazakhstan' ] gold = [ 13 , 11 , 10 , 9 , 8 , 8 , 6 , 5 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 ] silver = [ 11 , 5 , 10 , 7 , 7 , 6 , 3 , 0 , 8 , 4 , 1 , 4 , 3 , 7 , 4 , 2 , 4 , 3 , 1 , 0 , 0 , 2 , 2 , 2 , 1 , 0 ] bronze = [ 9 , 10 , 5 , 12 , 9 , 5 , 2 , 1 , 5 , 7 , 1 , 2 , 2 , 6 , 2 , 4 , 3 , 1 , 2 , 1 , 0 , 6 , 2 , 1 , 0 , 1 ] olympic_medal_counts = { 'country_name' :Series(countries), 'gold' : Series(gold), 'silver' : Series(silver), 'bronze' : Series(bronze)} olympic_medal_counts_df = DataFrame(olympic_medal_counts) bronze_at_least_one_gold = olympic_medal_counts_df[ 'bronze' ][olympic_medal_counts_df[ 'gold' ] > = 1 ] avg_bronze_at_least_one_gold = numpy.mean(bronze_at_least_one_gold) print (avg_bronze_at_least_one_gold) |