0

I have some data in text file, I want to repeat it multiple times. For example 1000 records

CustomerID, FirstName,LastName,phone

1,Orlando,Gee,245-555-0173   
2,Keith,Harris ,170-555-0127  
3,Donna,Carreras ,279-555-0130   
4,Janet,Gates,710-555-0173   
5,Lucy,Harrington,828-555-0186   
6,Rosmarie,Carroll,244-555-0112

I want output like

CustomerID, FirstName,LastName,phone

1,Orlando,Gee,245-555-0173   
2,Keith,Harris ,170-555-0127  
3,Donna,Carreras ,279-555-0130   
4,Janet,Gates,710-555-0173   
5,Lucy,Harrington,828-555-0186   
6,Rosmarie,Carroll,244-555-0112
7,Orlando,Gee,245-555-0173   
8,Keith,Harris ,170-555-0127  
9,Donna,Carreras ,279-555-0130   
10,Janet,Gates,710-555-0173   
11,Lucy,Harrington,828-555-0186   
12,Rosmarie,Carroll,244-555-0112
--------------------
----------------------
994,Orlando,Gee,245-555-0173   
995,Keith,Harris ,170-555-0127  
996,Donna,Carreras ,279-555-0130   
997,Janet,Gates,710-555-0173   
998,Lucy,Harrington,828-555-0186   
999,Rosmarie,Carroll,244-555-0112

How can i proceed on this

mohan111
  • 8,633
  • 4
  • 28
  • 55

1 Answers1

0

First read your text file into pandas dataframe:

import pandas as pd
pd_df = pd.read_csv('untitled.txt')
pd_df 



CustomerID  FirstName   LastName    phone
1           Orlando     Gee         245-555-0173
2           Keith       Harris      170-555-0127
3           Donna       Carreras    279-555-0130
4           Janet       Gates       710-555-0173
5           Lucy        Harrington  828-555-0186
6           Rosmarie    Carroll     244-555-0112

Then manipulate and write it back to text:

pd_df = pd_df.append([pd_df]*int(1000/6),ignore_index=True)
pd_df.loc[:,'CustomerID'] = range(1, pd_df.shape[0]+1)
pd_df = pd_df[pd_df.CustomerID<1000]
pd_df.to_csv('output.txt', index=False)
Ala Tarighati
  • 3,507
  • 5
  • 17
  • 34