Reputation: 821
My input dataframe looks like:
name uniqueID
kate 0001
sam 0001
lucy 0002
wes 0001
kip 0002
I have the following:
addData =pd.read_csv('/input.csv')
grouped = addData.groupby(['uniqueID'])
filename = addData['uniqueID'][0]
output_csv = '/test/output_{}.csv'.format(filename)
for name, group in grouped:
group.to_csv(output_csv)
My output is semi-correct. I have a file with all the associated records for that 'uniqueID'
, EX) output001.csv
:
name uniqueID
kate 0001
sam 0001
wes 0001
The problem is that I am only getting one file - my loop is not working correctly to produce both output0001.csv
and output0002.csv
Upvotes: 0
Views: 183
Reputation: 103
This worked:
grouped = addData.groupby(['uniqueID'])
filename = addData['uniqueID'][0]
output_csv = 'output_{}.csv'
for name, group in grouped:
group.to_csv(output_csv.format(name))
Upvotes: 2