0

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?

1
  • The thing you're "missing" is that you're overwriting the value of df on each iteration of the loop. In any case, the loop is not necessary, as shown in the answer already posted. Commented Apr 5, 2022 at 13:35

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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()

1 Comment

Shorter variant: pd.concat([pd.DataFrame.from_dict(value, orient='columns').assign(table_name=key) for key, value in my_dict.items()])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.