Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- def import_and_merge(circ_file, price_file):
- '''
- This function should read in the CSVs whose names are given by circ_file and price_file
- into separate Pandas dataframes.
- The circ_file and price_file are described by the circulation data and price data descriptions
- in the instructions.
- You should then merge those dataframes, and
- return a Pandas dataframe that is a combination of these two dataframes, i.e. each element (row)
- in its should be a 3-column (3-tuple) containing date, circulation, and price information.
- '''
- circ_df = pd.read_csv(circ_file)
- price_df = pd.read_csv(price_file)
- arranged_df = pd.merge(circ_df,price_df)
- return arranged_df
- pass
- def calculate_market_cap(data):
- '''
- This function should accept a Pandas dataframe containing date, circulation and price information,
- and should return a new column (a pd.series object) containing the market cap calculated using
- the formula in the instructions.
- '''
- x = data.Circulation * data.Price
- return x
- pass
- def get_max_row(data):
- '''
- This function should accept a Pandas dataframe containing date, circulation, price,
- and market cap information, with column names 'Date', 'Circulation', 'Price', and 'Market Cap',
- and should return the single row of that dataframe corresponding to the highest market cap
- (the day where the Bitcoin market capitalization was at its maximum).
- '''
- max_val_market_cap = data['Market Cap'].max()
- y = data.loc[data['Market Cap'] == max_val_market_cap]
- return y
- pass
- def format_row(row):
- '''
- This function should expect one row (with column headers),
- which is actually a 4-tuple/column dataframe
- (as described in the instructions for get_max_row())
- and should return a string that displays that row in the format presented in the grader output.
- '''
- my_tuple = row.iloc[0]
- a = str(my_tuple[0])
- b = str(my_tuple[1])
- c = str(my_tuple[2])
- d = str(my_tuple[3])
- command = 'On %s, circulation was %s and the exchange rate was %s, meaning the market cap was %s'%(a,b,c,d)
- return command
- pass
- if __name__ == '__main__':
- '''
- Your main function should import the two CSVs described in the instructions,
- merge them using your import_and_merge,
- then calculate the market cap for the data, and
- finally print out the row of the combined dataframe using you display_row function
- '''
- # First import the two CSVs described in the instructions that contain circulation
- # and price data using import_and_merge
- combined_df = import_and_merge('total_bitcoins.csv','bitcoin_price.csv')
- # Then use the merged dataframe to calculate the market cap for each date (data point/row)
- # using calculate_market_cap
- # Store the result in a new column (called 'Market Cap') of the existing dataframe
- new_df = calculate_market_cap(combined_df)
- new_df.columns = ['Market Cap']
- complete_df = pd.concat([combined_df,new_df], axis=1)
- complete_df.columns =['Date','Cirulation','Price','Market Cap']
- # Then find the row of the dataframe with the highest market cap using get_max_row
- max_row = get_max_row(complete_df)
- # Finally, print the max_row by printing the string returned by display_row
- print(format_row(max_row))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement