2

I am new to python and trying to work on merging multipe files in a desired layout. One of my problems involve repeating each row of the csv file (excluding header) n number of times. e.g

Initial file:

header1 header2 header3
abc       123    a1
def       456    b1
ghi       789    c1

After modification, the file should look like (e.g 3 repetitions):

header1 header2 header3
abc       123    a1
abc       123    a1
abc       123    a1
def       456    b1
def       456    b1
def       456    b1
ghi       789    c1
ghi       789    c1
ghi       789    c1

What is the best way to do it in python using either csv or pandas. Apologies if the question is too trivial but I am new to such file manipulations using python and didnt find any similar problem on forum.

Thanks,

Prune
  • 76,765
  • 14
  • 60
  • 81
Aisha
  • 91
  • 7
  • If you don't know where to start, trying reading the Python documentation for file I/O: https://docs.python.org/tutorial/inputoutput.html. And for the csv modle: https://docs.python.org/library/csv.html. Then try breaking the problem down into smaller parts and solving one part at a time – AbrahamCoding Feb 27 '19 at 02:11
  • See here for discussion of similar problem with multiple solutions: https://stackoverflow.com/questions/12130883/r-expand-grid-function-in-python – Robert Davy Feb 27 '19 at 02:50

2 Answers2

3

Using the csv module, you can do it as follows:

import csv

with open('out.txt', 'w') as fout, open('in.txt', 'r') as fin:
        reader = csv.reader(fin)
        writer = csv.writer(fout)
        writer.writerow(next(reader))
        for l in reader:
            for i in range(3):
                writer.writerow(l)
J. Taylor
  • 4,567
  • 3
  • 35
  • 55
1

for 3 times, you can do:

three_df = pd.concat([df,df,df])

you can easily write a loop that will do this n times.

Tacratis
  • 1,035
  • 1
  • 6
  • 16