在字典元组键值上拆分 dataframe/dictionary
Splitting dataframe/dictionary on dictionary tuple key value
你好,我有一个字典,它在每个时间间隔不断添加值,在这个大字典中,我有元组作为键,其中加密货币的符号、时间间隔和下一个值的名称都在元组中。
现在每个时间间隔都会添加值。在键元组包含“1m”的列表中,每 1m 将添加一个值,在键元组包含“3m”的列表中,将每 3 分钟添加一个值。
我想制作这本字典的数据框,但这不可能,因为列的长度不同。所以我想将这些 dictionaries/dataframes 分成 2 个独立的。
我的字典看起来像这样:
{('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']}
我想根据“3m”或“1m”值分隔 dictionary/dataframe。所以我想要一个 dictionary/dataframe 具有“1m”值和一个具有“3m”值。有谁知道我怎么能把这个和彼此分开?提前致谢!
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)
你好,我有一个字典,它在每个时间间隔不断添加值,在这个大字典中,我有元组作为键,其中加密货币的符号、时间间隔和下一个值的名称都在元组中。
现在每个时间间隔都会添加值。在键元组包含“1m”的列表中,每 1m 将添加一个值,在键元组包含“3m”的列表中,将每 3 分钟添加一个值。
我想制作这本字典的数据框,但这不可能,因为列的长度不同。所以我想将这些 dictionaries/dataframes 分成 2 个独立的。 我的字典看起来像这样:
{('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']}
我想根据“3m”或“1m”值分隔 dictionary/dataframe。所以我想要一个 dictionary/dataframe 具有“1m”值和一个具有“3m”值。有谁知道我怎么能把这个和彼此分开?提前致谢!
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)