How to pass each row of a dataFrame to an array

Viewed 291

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 ?

4 Answers

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

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}}]

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}}]

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}}]
Related