0

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

3
  • groupby and to csv Commented Dec 19, 2018 at 20:25
  • do: group.to_csv('/test/output_{}.csv'.format(group['uniqueID'][0])) Commented Dec 19, 2018 at 20:27
  • addData['uniqueID'][0] will only ever be a single value, 0001. Therefore output_csv will only ever be that value Commented Dec 19, 2018 at 20:28

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

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.