Home > OS >  Easiest/safest way to pass information cell by cell to a dataframe in pandas?
Easiest/safest way to pass information cell by cell to a dataframe in pandas?

Time:05-16

I have a very long algorithm I've been working on, and I would like to leave it running for days. I'd like to save the results and other variables to a dataframe for each loop and later pass them to a csv or xlsx file. For example, in this loop:

import random
import pandas as pd
import datetime as dt

for attempt in range(3):
    if attempt==0:
        string_class = 'First Class'
    else:
        string_class = 'Other Class'
    executing_alg = True
    while executing_alg:
        time_tst = dt.now()
        time.sleep(0.2)
        result_1 = random.random()
        result_2 = random.random()
        if result_1 > result_2:
            executing_alg=False

I would like to save the variables time_tst, result_1, result_2, and string_class. As a result, I would like to have a dataframe filled with these variables from each loop execution. Expected result:

Time Result_1 Result_2 Class
2022-05-16 02:35:11.069372 0.6570918119177281 0.043825063850461254 First Class
2022-05-16 02:35:11.479732 0.26843513214552006 0.010366867417694214 Other Class
2022-05-16 02:35:11.682106 0.9087965555346139 0.7634101174048724 Other Class

What comes to my mind is to make a list with the results and then append them to the series in the dataframe... but I guess there are easier and faster ways?... Thanks for any suggestion...

CodePudding user response:

This would write a new line in CSV format to a file for every loop. You can later read this easily with pandas, but there's no reason to use pandas to do this.

import random
import datetime as dt

with open('datafile.csv', 'a') as f:
    for attempt in range(3):
        if attempt==0:
            string_class = 'First Class'
        else:
            string_class = 'Other Class'
        executing_alg = True
        while executing_alg:
            time_tst = dt.now()
            time.sleep(0.2)
            result_1 = random.random()
            result_2 = random.random()
            if result_1 > result_2:
                executing_alg=False
        """
        It's not clear whether or not this next line
        should in inside your while loop or not.
        ...Adjust as needed:
        """
        f.write(f'{time_tst},{result_1},{result_2},{string_class}')
        f.write('\n')
  • Related