Home > Software design >  dataframe: create DataFrame using two different dictionaries
dataframe: create DataFrame using two different dictionaries

Time:10-20

I have two different functions which has two different dictionaries. First, I have to merge to dictionaries into one and then connect two dataframe.

import pandas as pd
output_df1 = {}
output_df1['diff_sum'] = 108
output_df1['cumsum'] = 232
out2 = {}
out2['carving'] = 1299
out2['bearing'] = 255
merge_dict = {**output_df1, **out2}
# upto this I'm able to do this
new_df = pd.DataFrame()
new_df['tata'] = merge_dict
new_df['diff_sum'] = 108
new_df

Getting result

enter image description here

Want this

enter image description here

How can I do it?

CodePudding user response:

You can also just convert the merge_dict to dataframe directly by setting the index name to 'tata' and then transposing it.

df2 = pd.DataFrame(merge_dict, index=['tata']).T
In [27]: df2
Out[27]: 
          tata
diff_sum   108
cumsum     232
carving   1299
bearing    255

CodePudding user response:

Try:

new_df = pd.DataFrame(merge_dict.values(), index=merge_dict, columns=["tata"])
print(new_df)

Prints:

          tata
diff_sum   108
cumsum     232
carving   1299
bearing    255

CodePudding user response:

This works for me:

import pandas as pd
output_df1 = {}
output_df1['diff_sum'] = 108
output_df1['cumsum'] = 232
out2 = {}
out2['carving'] = 1299
out2['bearing'] = 255
merge_dict = {**output_df1, **out2}
# upto this I'm able to do this
new_df = pd.DataFrame(merge_dict.values(),index=merge_dict.keys(),columns=['tata'])
new_df
  • Related