14

I want to write a list into a csv,When trying to do it I receive the below error

out.writerows(fin_city_ids)
_csv.Error: iterable expected, not numpy.int64

My code is as below

org_id.append([pol_id,bldest_id])
fin_ids=list(org_city_id['org_id'].unique())
print(fin_ids)

out = csv.writer(open("D:/dataset/fin_ids.csv","w"), delimiter='|')
out.writerows(fin_ids)

Below is the output from fin_ids

[1002774, 0, 1000702, 1000339, 1001620, 1000710, 1000202, 1003143, 147897, 31018, 1001502, 1002812, 1003026, 1003280, 1003289, 1002714, 133191, 5252218, 6007821, 1002632]

Org_id is a dataFrame which contains duplicate ids .fin_ids is a list which contains unqiue values of ids .Fin ID is a list of unique ids derived from the data frame org_id.

output desired is a CSV with all the values in separate rows as I am going to load the data into a sql table later .

6
  • 1
    What do you see when you print out fin_ids. Please add that here and show us what's contained org_id as well. csv is complaining because you're not providing it with a sequence for writerows.
    – Abdou
    Sep 2, 2016 at 0:07
  • 2
    Try outfile = open("D:/dataset/fin_ids.csv","w"); out = csv.writer(outfile, delimiter='|'); out.writerow(fin_ids); outfile.close(). Use writerow for one list or array.
    – Abdou
    Sep 2, 2016 at 0:13
  • well this just creates a kind of single row Sep 2, 2016 at 0:17
  • What's your desired output then?
    – Abdou
    Sep 2, 2016 at 0:18
  • updated the question with output details Sep 2, 2016 at 0:21

1 Answer 1

18

You can get this done in many ways. But if you wish to writerows from the csv module, then you will have to turn your list fin_ids into a sequence of lists first:

fin_ids = [1002774, 0, 1000702, 1000339, 
   1001620, 1000710, 1000202, 1003143, 147897, 
   31018, 1001502, 1002812, 1003026, 1003280, 
   1003289, 1002714, 133191, 5252218, 6007821, 1002632]

outfile = open('D:/dataset/fin_ids.csv','w')
out = csv.writer(outfile)
out.writerows(map(lambda x: [x], fin_ids))
outfile.close()

Another way would be to just use the .to_csv() method from pandas Series. Since you started with a dataframe, you could just do:

org_city_id['org_id'].unique().to_csv("D:/dataset/fin_ids.csv", index=False)

Both of these should generate a csv file with the following data:

1002774
0
1000702
1000339
1001620
1000710
1000202
1003143
147897
31018
1001502
1002812
1003026
1003280
1003289
1002714
133191
5252218
6007821
1002632

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.