Advertisement
Guest User

prelab12

a guest
Nov 15th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. def import_and_merge(circ_file, price_file):
  4. '''
  5. This function should read in the CSVs whose names are given by circ_file and price_file
  6. into separate Pandas dataframes.
  7. The circ_file and price_file are described by the circulation data and price data descriptions
  8. in the instructions.
  9. You should then merge those dataframes, and
  10. return a Pandas dataframe that is a combination of these two dataframes, i.e. each element (row)
  11. in its should be a 3-column (3-tuple) containing date, circulation, and price information.
  12. '''
  13. circ_df = pd.read_csv(circ_file)
  14. price_df = pd.read_csv(price_file)
  15. arranged_df = pd.merge(circ_df,price_df)
  16. return arranged_df
  17. pass
  18.  
  19.  
  20. def calculate_market_cap(data):
  21. '''
  22. This function should accept a Pandas dataframe containing date, circulation and price information,
  23. and should return a new column (a pd.series object) containing the market cap calculated using
  24. the formula in the instructions.
  25. '''
  26. x = data.Circulation * data.Price
  27. return x
  28. pass
  29.  
  30.  
  31. def get_max_row(data):
  32. '''
  33. This function should accept a Pandas dataframe containing date, circulation, price,
  34. and market cap information, with column names 'Date', 'Circulation', 'Price', and 'Market Cap',
  35. and should return the single row of that dataframe corresponding to the highest market cap
  36. (the day where the Bitcoin market capitalization was at its maximum).
  37. '''
  38. max_val_market_cap = data['Market Cap'].max()
  39. y = data.loc[data['Market Cap'] == max_val_market_cap]
  40. return y
  41. pass
  42.  
  43.  
  44. def format_row(row):
  45. '''
  46. This function should expect one row (with column headers),
  47. which is actually a 4-tuple/column dataframe
  48. (as described in the instructions for get_max_row())
  49. and should return a string that displays that row in the format presented in the grader output.
  50. '''
  51. my_tuple = row.iloc[0]
  52. a = str(my_tuple[0])
  53. b = str(my_tuple[1])
  54. c = str(my_tuple[2])
  55. d = str(my_tuple[3])
  56. command = 'On %s, circulation was %s and the exchange rate was %s, meaning the market cap was %s'%(a,b,c,d)
  57. return command
  58. pass
  59.  
  60.  
  61. if __name__ == '__main__':
  62. '''
  63. Your main function should import the two CSVs described in the instructions,
  64. merge them using your import_and_merge,
  65. then calculate the market cap for the data, and
  66. finally print out the row of the combined dataframe using you display_row function
  67. '''
  68. # First import the two CSVs described in the instructions that contain circulation
  69. # and price data using import_and_merge
  70. combined_df = import_and_merge('total_bitcoins.csv','bitcoin_price.csv')
  71. # Then use the merged dataframe to calculate the market cap for each date (data point/row)
  72. # using calculate_market_cap
  73. # Store the result in a new column (called 'Market Cap') of the existing dataframe
  74. new_df = calculate_market_cap(combined_df)
  75. new_df.columns = ['Market Cap']
  76. complete_df = pd.concat([combined_df,new_df], axis=1)
  77. complete_df.columns =['Date','Cirulation','Price','Market Cap']
  78. # Then find the row of the dataframe with the highest market cap using get_max_row
  79. max_row = get_max_row(complete_df)
  80. # Finally, print the max_row by printing the string returned by display_row
  81. print(format_row(max_row))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement