I have a problem with merging together serval dataframes. I downloaded some historical trading data and saved it into a csv file. So now I want to read the data from the cvs file into several dataframes and extract some close prices.
I created a function, called read_dataset that reads the data into a dataframe and returns that dataframe.
In combination with a for loop I stored all Dataframes in a dict. Dict keys are the abbreviations of the currency (see coin_list dataframe).
# List of available coins, saved in a DataFrame called coin_list
coins = { 'Bitcoin': 'BTC', 'Ethereum': 'ETH', 'Ripple': 'XRP', 'BitcoinCash': 'BCH', 'Litecoin':'LTC', 'EOS': 'EOS',
'Tronix': 'TRX', 'Stellar' : 'XLM', 'Neo' : 'NEO', 'Cardano': 'ADA', 'IOTA' : 'IOT', 'Monero': 'XMR'}
# Create a coin list as Dataframe of the dictionary above
coin_list = pd.DataFrame(list(coins.items()), index = np.arange(0,12), columns=('Currency', 'Abbreviation'), dtype=str)
# Read data into DataFrames
def read_dataset (filename):
print('Reading data from %s' % filename)
file = pd.read_csv(filename)
file = file.drop('Unnamed: 0', axis=1)
return file
# Read all cryptocurrency data into a dictionary of dataframes.
currency_data = {}
df = pd.DataFrame()
for currency in coin_list['Abbreviation']:
df = read_dataset(currency + '_historical_data_daily_updated')
df = df.set_index('Timestamp')
currency_data[currency] = df
currency_data
Out:
{'ADA': close high low open volumefrom volumeto
Timestamp
2017-12-30 0.5900 0.6941 0.4200 0.4955 24118261.70 14016860.69
2017-12-31 0.7100 0.7400 0.5900 0.5900 13107255.34 8971147.70
2018-01-01 0.7022 0.7150 0.6320 0.7100 13805601.70 9403559.91
2018-01-02 0.7620 0.8000 0.6750 0.7022 8440669.40 6292466.84
So after the dict currency_data is created I want to access and separate the dataframes contained in currency_data. Therefore I want to create a for loop to merge for example all close prices of the data frames into one dataframe.
Has anyone an idea how I can achieve that?
I can do this for two dataframes with the following code, but can't translate that into a for loop.
a = pd.DataFrame()
a['ADA closeprice'] = currency_data['ADA']['close']
b = pd.DataFrame()
b['BTC closeprice'] = currency_data['BTC']['close']
c = pd.merge(a, b, left_index=True, right_index=True)
c.drop_duplicates()
c.head()
ADA closeprice BTC closeprice
Timestamp
2017-12-30 0.5900 12531.52
2017-12-31 0.7100 13850.40
2018-01-01 0.7022 13444.88
2018-01-02 0.7620 14754.13
2018-01-03 1.1000 15156.62
Or is there a better way to create different dataframes from a cvs file and storing it in a dict?
Thanks for your help!