5

How to add a dataframe row in a field array Like i have my data frame.

import pandas as pd
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
print df

Output:

   c1   c2
0  10  100
1  11  110
2  12  120

So i want to have something like this :

{
  "fields": {
        "c1": 10,
        "c2": 100,
   }
},
{
  "fields": {
        "c1": 11,
        "c2": 110,
   }
},
{
  "fields": {
        "c1": 12,
        "c2": 120,
   }
}

How can i do it ?

Ekane 3
  • 160
  • 1
  • 12

4 Answers4

6

You can do:

a = df.transpose().to_dict()
a
>>> {0: {'c1': 10, 'c2': 100}, 1: {'c1': 11, 'c2': 110}, 2: {'c1': 12, 'c2': 120}}
res = [{'fields': a[i]} for i in a]
res
>>> [{'fields': {'c1': 10, 'c2': 100}}, {'fields': {'c1': 11, 'c2': 110}}, {'fields': {'c1': 12, 'c2': 120}}]

As @anky points out, defining a like so: a = df.to_dict('index') will also work, not sure which is more computationally efficient

user15270287
  • 1,123
  • 4
  • 11
4

You can try using df.to_dict with orient as records.

out = df.to_dict(orient='records')
# [{'c1': 10, 'c2': 100}, {'c1': 11, 'c2': 110}, {'c1': 12, 'c2': 120}]
out = [{'fields': val} for val in out]

[{'fields': {'c1': 10, 'c2': 100}},
 {'fields': {'c1': 11, 'c2': 110}},
 {'fields': {'c1': 12, 'c2': 120}}]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
3

Try chain with df.to_dict

d = [{'field' : x} for x in df.to_dict('records')]
Out[167]: 
[{'field': {'c1': 10, 'c2': 100}},
 {'field': {'c1': 11, 'c2': 110}},
 {'field': {'c1': 12, 'c2': 120}}]
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I guess using short-forms is scheduled for deprecation in the next version. Might want to mention it as in newer versions it might raise `Exception`, the latest version throws `FutureWarning`. Works like charm in older versions. – Ch3steR Mar 07 '21 at 17:06
  • Thanks BENY, but 'r' parameter is deprecated as Ch3steR said.So i changed the 'r' to 'records' and it worked perfectly .Thanks – Ekane 3 Mar 07 '21 at 23:04
1

Pandas' built in method to_dict() allows converting a dataframe to a serialized list of records. Your desired output would require a transformation on these records:

# Get each row as a record
records = df.to_dict(orient='records')
# [{'c1': 10, 'c2': 100}, {'c1': 11, 'c2': 110}, {'c1': 12, 'c2': 120}]

# Transform each row
records = [{'fields':x} for x in records]
# [{'fields': {'c1': 10, 'c2': 100}},
# {'fields': {'c1': 11, 'c2': 110}},
# {'fields': {'c1': 12, 'c2': 120}}]
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69