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 ?
Use apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply(). By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.
You can convert select columns of a dataframe into an numpy array using the to_numpy() method by passing the column subset of the dataframe.
In order to iterate over rows, we apply a function itertuples() this function return a tuple for each row in the DataFrame. The first element of the tuple will be the row's corresponding index value, while the remaining values are the row values.
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}}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With