Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. from pandas import DataFrame, Series
  2. import numpy
  3.  
  4.  
  5. def avg_medal_count():
  6. '''
  7. Compute the average number of bronze medals earned by countries who
  8. earned at least one gold medal.
  9.  
  10. Save this to a variable named avg_bronze_at_least_one_gold.
  11.  
  12. HINT-1:
  13. You can retrieve all of the values of a Pandas column from a
  14. data frame, "df", as follows:
  15. df['column_name']
  16.  
  17. HINT-2:
  18. The numpy.mean function can accept as an argument a single
  19. Pandas column.
  20.  
  21. For example, numpy.mean(df["col_name"]) would return the
  22. mean of the values located in "col_name" of a dataframe df.
  23. '''
  24.  
  25.  
  26. countries = ['Russian Fed.', 'Norway', 'Canada', 'United States',
  27. 'Netherlands', 'Germany', 'Switzerland', 'Belarus',
  28. 'Austria', 'France', 'Poland', 'China', 'Korea',
  29. 'Sweden', 'Czech Republic', 'Slovenia', 'Japan',
  30. 'Finland', 'Great Britain', 'Ukraine', 'Slovakia',
  31. 'Italy', 'Latvia', 'Australia', 'Croatia', 'Kazakhstan']
  32.  
  33. 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]
  34. 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]
  35. 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]
  36.  
  37. olympic_medal_counts = {'country_name' : Series(countries), 'gold' : Series(gold), 'silver' : Series(silver),
  38. 'bronze' : Series(bronze)}
  39.  
  40. olympic_medal_counts_df = DataFrame(olympic_medal_counts)
  41. #avg_bronze = olympic_medal_counts_df['bronze'].apply(numpy.mean)
  42. return olympic_medal_counts_df
  43. #return avg_bronze_at_least_one_gold
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement