Getting an error about iteration while trying to write list to a CSV

So I am currently trying to write a list of numbers as a new column into an existing CSV file and am having issues. The error I am getting is that the int must be iterable.

with open('AB_NYC_2019.csv', 'w+', newline ='') as f:
write = csv.writer(f)
for x in range(len(pricelist)):
    write.writerows(x)

I also tried doing it like this and got the error that iterable expected, not Int.

with open('AB_NYC_2019.csv', 'w+', newline ='') as f:
    write = csv.writer(f)
    write.writerows(pricelist)

the pricelist list which I am trying to write into the CSV looks like this (Much longer but here is a shortened version)

[163, 248, 166, 112, 100, 226, 70, 85, 96, 165, 146, 94, 100, 101, 147, 161, 241, 167, 124, 209, 325, 142, 98, 138, 133, 67, 107, 161, 56, 205, 67, 57, 71, 75, 93, 113, 44, 112, 170, 47, 90, 142, 138, 163, 167, 159, 138, 128, 134, 105, 87, 168, 234, 155, 216, 156, 120, 90, 62, 199, 88, 398, 269, 218, 81, 66, 234, 99, 305, 129, 242, 235, 66, 91, 130, 201, 218, 98, 110, 168, 151, 134, 295, 142, 102, 828, 127, 75, 60, 115, 76, 155, 128, 267, 109, 110, 162, 104, 106, 155, 90, 190, 88, 510, 125, 241, 148, 92, 112, 222, 66, 136, 165, 148, 379, 211, 346, 252, 246, 114, 183, 421, 198, 118, 101, 112, 164, 99, 96, 126, 101, 192, 61, 112, 156, 130, 101, 106, 151, 147, 270, 209, 168, 145, 132, 182, 219, 206, 94, 73, 170, 111, 142]

I also am not sure how to make a new column for it

Upvotes: 0

Views: 113

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9629

Why not use pandas?

import pandas as pd

df = pd.read_csv('AB_NYC_2019.csv') # load file
df['pricelist'] = pricelist # assign list to column
df.to_csv('AB_NYC_2019.csv', index=False, header=False) # save file

Upvotes: 0

Related Questions