I have the following data that I want convert into pandas dataframe
Input
my_dict = {'table_1': [{'columns_1': 148989, 'columns_2': 437643}], 'table_2': [{'columns_1': 3344343, 'columns_2': 9897833}]}
Expected Output
table_name columns_1 columns_2
table_1 148989 437643
table_2 3344343 9897833
I tried below way but due to the loop, i can only get the last value
def convert_to_df():
for key, value in my_dict.items():
df = pd.DataFrame.from_dict(value, orient='columns')
df['table_name'] = key
return df
What I’m I missing?
>Solution :
Just get rid of those lists and you can feed directly to the DataFrame constructor:
pd.DataFrame({k: v[0] for k,v in my_dict.items()}).T
output:
columns_1 columns_2
table_1 148989 437643
table_2 3344343 9897833
With the index as column:
(pd.DataFrame({k: v[0] for k,v in my_dict.items()})
.T
.rename_axis('table_name')
.reset_index()
)
output:
table_name columns_1 columns_2
0 table_1 148989 437643
1 table_2 3344343 9897833