How to export a series of text strings to csv via Python?

Viewed 30

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?

1 Answers

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)
Related