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 ?

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This method changes my column date to timestamp.From 2020-01-03 to Timestamp('2020-01-03 00:00:00'),How can i fix that
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}}]

Comments

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

2 Comments

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.
Thanks BENY, but 'r' parameter is deprecated as Ch3steR said.So i changed the 'r' to 'records' and it worked perfectly .Thanks
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}}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.