Home > Enterprise >  Splitting dataframe/dictionary on dictionary tuple key value
Splitting dataframe/dictionary on dictionary tuple key value

Time:11-18

Hello I have a dictionary which keeps adding values every interval, In this large dictionary I have tuples as keys in which the symbol of the cryptocurrency, the interval and the name of the next values are in the tuples.

Now values will be added every interval. in the list where the key tuple contains '1m' there will be values added every 1m and in the list where the key tuple contains '3m'there will be values added every 3minutes.

I want to make a dataframe of this dictionary but that becomes impossible because the columns won't be of the same length. So I wanted to split these dictionaries/dataframes into 2 seperate ones. My dictionary looks like this:

{('BTCUSDT', '3m', 'high'): [60607.54, 60642.85, 60548.8, 60663.06, '60672.13000000'], ('BTCUSDT', '3m', 'low'): [60524.07, 60488.83, 60441.21, 60400.0, '60507.84000000'], ('BTCUSDT', '3m', 'close'): [60556.68, 60500.48, 60470.65, 60636.97, '60530.13000000'], ('BTCUSDT', '1m', 'high'): [60473.25, 60534.04, 60663.06, 60672.13, '60624.06000000', '60582.67000000', '60571.22000000'], ('BTCUSDT', '1m', 'low'): [60400.0, 60434.62, 60520.5, 60600.0, '60548.88000000', '60507.84000000', '60530.13000000'], ('BTCUSDT', '1m', 'close'): [60450.75, 60524.53, 60636.97, 60606.02, '60576.00000000', '60530.13000000', '60562.95000000']}

I want to seperate the dictionary/dataframe based on the '3m' or '1m' value. So I want one dictionary/dataframe with '1m' values and one with '3m' values. Does anyone know how I can seperate this from eachother? Thanks in advance!

CodePudding user response:

import pandas as pd 

dictionary = {
     ('BTCUSDT', '3m', 'high'): [60607.54, 60642.85, 60548.8, 60663.06, '60672.13000000'], 
     ('BTCUSDT', '3m', 'low'): [60524.07, 60488.83, 60441.21, 60400.0, '60507.84000000'], 
     ('BTCUSDT', '3m', 'close'): [60556.68, 60500.48, 60470.65, 60636.97, '60530.13000000'], 
     ('BTCUSDT', '1m', 'high'): [60473.25, 60534.04, 60663.06, 60672.13, '60624.06000000', '60582.67000000', '60571.22000000'], 
     ('BTCUSDT', '1m', 'low'): [60400.0, 60434.62, 60520.5, 60600.0, '60548.88000000', '60507.84000000', '60530.13000000'], 
     ('BTCUSDT', '1m', 'close'): [60450.75, 60524.53, 60636.97, 60606.02, '60576.00000000', '60530.13000000', '60562.95000000']
}

# Filter out the keys 
onemdict_keys = [key for key in dictionary.keys() if key[1]=='1m']
threemdict_keys = [key for key in dictionary.keys() if key[1]=='3m']

# Use keys to gather values 
onem_values = [dictionary[key] for key in onemdict_keys]
threem_values = [dictionary[key] for key in threemdict_keys]

# Separate dictionaries (zipping two lists creates a dictionary)
onem_dict = zip(onemdict_keys, onem_values)
threem_dict = zip(threemdict_keys, threem_values)

# Separate dataframes (pass dictionary to pandas to create a dataframe)
onemdf = pd.DataFrame(onem_dict)
threemdf = pd.DataFrame(threem_dict)
  • Related