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?
>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`?