0

This question has been asked before but those solutions didn't work for me. I have a csv file, example.csv, with data in the following format:

grid,u,lambda
0.0,0.0,-93000
0.01,-0.01,-93000
0.02,0.05,-93000

Now I have a function f.fraction('function') which returns me [1.00000000e+00 1.00000000e+00 1.00000000e+00]. I want to append this to the same file, example.csv. the final result:

grid,u,lambda,new_function
0.0,0.0,-93000,1.00000000e+00
0.01,-0.01,-93000,1.00000000e+00
0.02,0.05,-93000,1.00000000e+00

How can I do this?

1 Answer 1

1

you can read the file first using pandas, append the new column and then save it.

import pandas as pd
df = pd.read_csv('example.csv')
df['new_function'] = f.fraction('function')
df.to_csv('example.csv')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this solution. This worked perfectly fine. But, if you don't mind can you also provide the solution without using pandas as well, please?. I mean by using import csv. @diego-ramirez-vasquez
This post will help you to do it without pandas :) stackoverflow.com/questions/60465764/…
Thanks again! @Diego Ramirez Vasquez

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.