Combining similar dictionaries into a framework in pandas

How to create a pandas DataFrame from two or more dictionaries having common keys? That is, to convert

 d1 = {'a': 1} d2 = {'a': 3} ... 

into a data frame with columns ['d1', 'd2', ...] , rows indexed as "a" , and values ​​defined by the corresponding dictionaries?

+5
source share
1 answer
 import pandas as pd d1 = {'a': 1, 'b':2} d2 = {'a': 3, 'b':5} df = pd.DataFrame([d1, d2]).T df.columns = ['d{}'.format(i) for i, col in enumerate(df, 1)] 

gives

 In [40]: df Out[40]: d1 d2 a 1 3 b 2 5 
+4
source

Source: https://habr.com/ru/post/1202438/


All Articles