Home > Mobile >  How to convert python dictionary to pandas dataframe
How to convert python dictionary to pandas dataframe

Time:04-06

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?

CodePudding user response:

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

CodePudding user response:

Not the nicest way imho (mozway's method is nicer), but to continue on the road you tried, you need to add the output of your for loop to a list and then concat that into 1 dataframe.

def convert_to_df():
    df_list = []  #Add a list where the output of every loop is added to
    for key, value in my_dict.items()
        df = pd.DataFrame.from_dict(value, orient='columns')
        df['table_name'] = key
        df_list.append(df)  #Append to the list
    df = pd.concat(df_list)  # Concat list into dataframe
    return df

df = convert_to_df()
  • Related