Friday, June 3, 2022

AI/ML: Various ways for converting dictionary to dataframe

np.random.seed(0)

data = pd.DataFrame(np.random.choice(10, (3, 4)), columns=list('ABCD')).to_dict('r')



# The following methods all produce the same output.

pd.DataFrame(data)

pd.DataFrame.from_dict(data)

pd.DataFrame.from_records(data)



Dictionary Orientations: orient='index'/'columns'

There are two primary types: "columns", and "index".


orient='columns'

Dictionaries with the "columns" orientation will have their keys correspond to columns in the equivalent DataFrame.


pd.DataFrame.from_dict(data_c, orient='columns')

  A  B  C  D

0  5  0  3  3

1  7  9  3  5

2  2  4  7  6


pd.DataFrame.from_records, the orientation is assumed to be "columns" (you cannot specify otherwise), and the dictionaries will be loaded accordingly.



orient='index'

With this orient, keys are assumed to correspond to index values. This kind of data is best suited for pd.DataFrame.from_dict.


data_i ={

 0: {'A': 5, 'B': 0, 'C': 3, 'D': 3},

 1: {'A': 7, 'B': 9, 'C': 3, 'D': 5},

 2: {'A': 2, 'B': 4, 'C': 7, 'D': 6}}



pd.DataFrame.from_dict(data_i, orient='index')


   A  B  C  D

0  5  0  3  3

1  7  9  3  5

2  2  4  7  6


Setting Custom Index

If you need a custom index on the resultant DataFrame, you can set it using the index=... argument.


pd.DataFrame(data, index=['a', 'b', 'c'])

# pd.DataFrame.from_records(data, index=['a', 'b', 'c'])


   A  B  C  D

a  5  0  3  3

b  7  9  3  5

c  2  4  7  6


This is not supported by pd.DataFrame.from_dict.


https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe


No comments:

Post a Comment