Я создал словарь следующим образом:
Данные выглядят так:
GDS3:
ABC_1 ABC_2 BBB_1
cat elf 123
dog run 456
bird burp 789
GDS4:
ABC_3 ABC_4 BCB_a
beer yes 234
wine no 543
gin yes 743
GDS5:
ABC_5 ABC_6 BCD_c
lol yea 543
lmao NaN 446
asl NaN 777
#create a dictionary in which all columns that start with the same 3 characters will be grouped in the same key.
dict_2013 = {k: g for k, g in GDS3.groupby(by=lambda x: x[:3].lower(), axis=1)}
dict_2014 = {k: g for k, g in GDS4.groupby(by=lambda x: x[:3].lower(), axis=1)}
dict_2015 = {k: g for k, g in GDS5.groupby(by=lambda x: x[:3].lower(), axis=1)}
#start with year 2013:
global_dict=dict_2013
#if key in the new dictionary is in the old dictionary then
#add the values from the new dictionary key to the old dictionary key
#else if the new dictionary key does not exist in the old dictionary then add a new key with the new values
for key,val in dict_2014.items():
if key in global_dict:
global_dict[key]=[global_dict[key],val]
else:
global_dict[key]=val
for key,val in dict_2015.items():#to add items
if key in global_dict:
global_dict[key]=[global_dict[key],val]
else:
global_dict[key]=val
Это мой желаемый вывод (кадр данных для каждой клавиши)
df_ABC:
ABC_1 ABC_2 ABC_3 ABC_4 ABC_5
cat elf beer yes lol
dog run win no lmao
bird burp gin yes asl
df_BBB:
BBB_1
cat
dog
bird
Другими словами, я хочу преобразовать отдельные ключи в отдельные словари (ДЛЯ ВСЕХ КЛЮЧЕЙ), поэтому я попробовал следующее:
ABC_dataframe=pd.DataFrame(global_dict['ABC'])
Когда я делаю это, я получаю следующую ошибку:
TypeError: Expected list, got DataFrame
Что странно, потому что global_dict ['ABC'] - это список. (Я проверил, используя тип (global_dict ['ABC']).
Что я могу сделать, чтобы исправить это? Я попытался сгладить список, но у меня все еще есть проблемы.