Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass each row of a dataFrame to an array

Tags:

python

pandas

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 ?

like image 441
Ekane 3 Avatar asked Mar 07 '21 16:03

Ekane 3


People also ask

How do I apply a function to all rows of a data frame?

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.

How do you convert a column of data to an array in Python?

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.

How do I iterate over a data frame?

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.


Video Answer


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

like image 155
user15270287 Avatar answered Oct 17 '22 00:10

user15270287


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}}]
like image 4
Ch3steR Avatar answered Oct 17 '22 01:10

Ch3steR


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}}]
like image 3
BENY Avatar answered Oct 17 '22 02:10

BENY


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}}]
like image 1
Yaakov Bressler Avatar answered Oct 17 '22 02:10

Yaakov Bressler