0

I have a python script that runs a series of checks on a pandas dataframe, and prints the results of each check. The code behind each check looks a bit like this:

if sum(df["column1"]) >0:
    print("Success")
else:
    print("Failure")

What I want to do is save the results to a csv file with each result in its own cell, so that the csv looks like this:

Success
Success
Failure
Success
Failure

Does anyone know how to do this please?

SRJCoding
  • 335
  • 2
  • 15
  • Are you running this if statement in for loop? – Moses Sep 16 '22 at 14:32
  • Does this answer your question? [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Yevhen Kuzmovych Sep 16 '22 at 14:33
  • Your output is not formatted as csv, it's just a column of single words. Can't you use the [default writing method](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)? – Ignatius Reilly Sep 16 '22 at 14:38

1 Answers1

1

You can save the results as a List and export them as pandas DataFrame to csv:

res=[]
if sum(df["column1"]) >0:
    res.append("Success")
else:
    res.append("Failure")

pd.DataFrame(res).to_csv('filename.csv',index=False,header=False)