Reputation: 579
I have a Dataframe df with 11 rows and 1 column. In each row I have an array that I would convert into json but assigning to each entry in array a key.
For example,
0
0 [1.234,1.234,2.1123,4.32212,1.2231,2.55323,1.4421]
1 [2.21,1.234,1.31,3.121,2.22,2.32322,0.8873]
The above DataFrame should become the same DataFrame but having json data in the entries:
0
0 {"0":1.234,"1":1.234,"2":2.1123,"3":4.32212,"4"1.2231,"5"2.55323,"6":
1.4421}
1 {"0":2.21,"1":1.234,"2":1.31,"3":3.121,"4":2.22,"5":2.32322,"6":0.8873}
Thanks in advance.
Upvotes: 0
Views: 878
Reputation: 214927
Use apply
method to loop through the column cells; for each item(list), use enumerate
to add the index (key) and then convert it to a dictionary:
df['0'].apply(lambda lst: dict(enumerate(lst)))
#0 {0: 1.234, 1: 1.234, 2: 2.1123, 3: 4.32212, 4:...
#1 {0: 2.21, 1: 1.234, 2: 1.31, 3: 3.121, 4: 2.22...
#Name: 0, dtype: object
Upvotes: 2