Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading and writing a csv file in one line

I have below code

import pandas as pd
(pd.read_csv('https://www.stats.govt.nz/assets/Uploads/Business-financial-data/Business-financial-data-September-2022-quarter/Download-data/business-financial-data-september-2022-quarter-csv.zip')
    .assign(New = lambda x : 'NEW')
    .apply(lambda x : x.to_csv(x['New'].values[0] + '_File.csv')))

Basically, I wanted to use chain rule to read and write a csv file, after some modification using above lines of code. Final file name is chosen dynamically based on some value of chosen column. However this unfortunately is failing.

Could you please help to correct above code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use pipe :

(pd.read_csv('https://www.stats...quarter-csv.zip')
    .assign(New = lambda x : 'NEW')
    .pipe(lambda x: x.to_csv(x['New'].iat[0] + '_File.csv'))) # with `index=False`?
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading