2

I'm trying to write the data from my list to just column 4

namelist = ['PEAR']

for name in namelist:
    for man_year in yearlist:
        for man_month in monthlist:
            with open('{2}\{0}\{1}.csv'.format(man_year,man_month,name),'w') as filename:
                writer = csv.writer(filename)
                writer.writerow(name)
                time.sleep(0.01)

it outputs to a csv like this

P             E                 A                R

4015854     234342            2442343            234242

How can I get it to go on just the 4th column?

                                                        PEAR

4015854     234342                2442343              234242
iOSecure
  • 232
  • 6
  • 18

3 Answers3

3

Replace the line writer.writerow(name) with,

writer.writerow(['', '', '', name])
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
1

When you pass the name to csvwriter it assumes the name as an iterable and write each character in a column.

So, for getting ride of this problem change the following line:

writer.writerow(name)

With:

writer.writerow([''] * (len(other_row)-1) + [name])

Here other_row can be one of the rest rows, but if you are sure about the length you can do something like:

writer.writerow([''] * (length-1) + [name])
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

Instead of writing '' to cells you don't want to touch, you could use df.at instead. For example, you could write df.at[index, ColumnName] = 10 which would change only the value of that specific cell.

You can read more about it here: Set value for particular cell in pandas DataFrame using index

j413254
  • 173
  • 1
  • 10