2

I have written the following code and I want to write the output into a csv file. How can I do this? Thanks for helping me. Here is an extract from my code:

columnNames = ['Stride Length', 'Stand Duration', 'Swing Duration', 'Douple Support Time', 'Relation Swing Stand']

resultsPerRowRatio = list()

for i in range(len(stepData)):
     stepDataLeft = stepData.to_numpy()[i, 0:5]
     stepDataRight = stepData.to_numpy()[i, 5:10]
     dataF = pd.DataFrame({'stepDataLeft': stepDataLeft, 'stepDataRight': stepDataRight}, index=columnNames)

     #Symmetry Ratio:
     dataF['Symmetry Ratio Row']=np.where(dataF['stepDataLeft'] < dataF['stepDataRight'],sv.symmetryRatio(dataF['stepDataLeft'],dataF['stepDataRight']), sv.symmetryRatio(dataF['stepDataRight'],dataF['stepDataLeft']))
     resultsPerRowRatio.append(dataF)

print("Symmetry Ratio: "+str(resultsPerRowRatio))

This is my output:

enter image description here

I want to write this in a csv file. But I don't know how. I have already tried various options.

1

1 Answer 1

2

This is an approach to consider using to_csv():

to_csv() Parameters(more are available, but this is simple case):

  • header: column header, True to include or False to exclude
  • mode: 'a' to append to an existing file, 'w' for creating and writing a new file
  • index: True to include indexing, False to exclude

Example:

PATH = your_folder_path
FILENAME = output_file_name

if os.path.isfile(PATH+FILENAME):
    dataF.to_csv(PATH+FILENAME, header=False, mode='a', index=True)
else:
    dataF.to_csv(PATH+FILENAME, header=True, mode='w', index=True)

This structure will do a logic check to see if your output file already exists in the folder path you've defined, if it does exist, it will append the data to the csv file without the associated header column names via the to_csv() call. If it doesn't exist, it will generate a new csv file and include the column names as a header.

Sign up to request clarification or add additional context in comments.

1 Comment

Any other alternative method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.