How do you loop through a dataframe and convert rows to JSON objects

de19

I am creating a function that sends data to a remote server. I am currently using the pandas library to read through a CSV file and convert the data to a dataframe. What I need to do is loop through that dataframe and covert each row to JSON and send them to my database.

The reason I need to loop through is that sets of data that are too big (currently sending 100 row by 21 col) are too long for HTML strings. What I need to do is send loop through and send lots of 10 or so.

Below is where I am at the moment:

def UploadData(root, self, data):
        i = 0
        data_arr = []
        for row in data:
            if i % 5 == 0:
                # Add row to array or something
                data_arr.append(row)
                json_str = data_arr.to_json(orient='records')
                url = 'https://newsimland.com/~db/JSON/?tok={"tok":"YOUR TOKEN HERE","cmd":{"STORE":"test_database","VALUE":'+ json_str +'}}'
                r = requests.get(url)
            else:
                # Add row to array
                data_arr.append(row)
            i += 1
        data = r.json()
        if r.status_code == 200:
            Alert(title="Error", text="Data upload unsuccessful")
        else:
            Alert(title="Success", text="Data upload successful")

One of the problems with this is that .to_json(orient='records') is meant for a dataframe, not the array I am appending to. Also if the original dataframe is less than 5 rows, it wont send the data to the database.

Does anyone know how I could achieve this?

Detlef

If I understand you correctly, you want to send your DataFrame in parts of 5 or fewer rows. In this case I recommend you to split the dataframe in the following way, so that the rows are retained as a DataFrame and you can use to_json.

import numpy as np

def UploadData(root, self, data):
    size = 5
    for chunk in np.split(data, np.arange(size, data.size, size)):
        if chunk.size:
            json_str = chunk.to_json(orient='records')
            # Send your data here! 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you loop through different rows (bootstrap rows) in Angular?

How do you loop through a list of objects and get a specific attribute?

How do you convert a list of json objects to a list of javascript objects?

How do you convert a PHP Loop into JSON Loop?

How do you combine data in a Scala dataframe and output it as JSON objects?

convert csv to json objects and loop through each of the objects not working

How do you pass a JSON object through a snowflake stored proc then loop through the key values

How to loop through json data with multiple objects

How to loop through nested json objects in swift?

How do you loop through api with list of parameters and store resulting calls in one dataframe

How do you create objects using a for loop

How do I loop through API keys to get output for them in separate rows in my final dataframe?

How do you transpose a dask dataframe (convert columns to rows) to approach tidy data principles

how do you convert a for loop output into a list?

Convert an Spark dataframe columns with an array of JSON objects to multiple rows

Loop through subsets of rows in a DataFrame

Iterate through Dataframe Rows in Loop

Python - How to convert an array of json objects to a Dataframe?

How to convert json to Pandas Dataframe with nested objects?

How do I loop through json and generate table rows? jsbin attached

I have a .log file of objects how do I convert them to a JSON object for iterating through in Javascript?

How to Loop through JSON Objects having Objects and Arrays Inside

How do you convert this Excel table/sheet into an array of JSON objects using Python3?

How do you render objects from json

How do you assign json objects in jq?

How do you parse a JSON array of objects?

How to loop through rows in dataframe while using google API

How to loop through Pandas DataFrame and split a string into multiple rows

How do I loop through this JSON object?