Creating a list of dicts from Pandas df

I have pandas dataframe forms

0   x     y    z
1   .5   .1    4
2   .6   .2    5

I want to convert this to a list of dicts for the first two cols, namely [{'x': 0.5, 'y': 0.1}, {'x': 0.6, 'y': 0.2} .....]

I can write a loop and do it in a silly way, is there a faster way?

+4
source share
3 answers

You can use iterrows . This allows you to Seriesiterate over strings like , rather than dicts, but it is pretty similar (e.g. has iteritems(), __getitem__etc.).

If you must use dicts, you can easily convert each Seriesto dict using the method to_dict().

For example:

list_of_dicts = list( row.to_dict() for key, row in df.iterrows() )
+3

to_dict(). yourdata.csv - .csv:

df = pd.read_csv('yourdata.csv')

d = df[['x','y']].to_dict('index').values()

. :

[{'y': 0.1, 'x': 0.5}, {'y': 0.2, 'x': 0.6}]
0

to_dict(orient='records') orient=records .

In [2]: df[['x', 'y']].to_dict(orient='records')
Out[2]:
[{'x': 0.5, 'y': 0.1}, {'x': 0.6, 'y': 0.2}]

In [8]: df.shape
Out[8]: (10000, 4)

In [9]: %timeit df[['x', 'y']].to_dict(orient='records')
10 loops, best of 3: 68.4 ms per loop

In [10]: %timeit df[['x','y']].to_dict('index').values()
1 loop, best of 3: 570 ms per loop 

In [11]: %timeit list(row.to_dict() for key, row in df[['x', 'y']].iterrows())
1 loop, best of 3: 575 ms per loop
0
source

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


All Articles