Reputation: 333
I have 8 different .txt files, all in the same format and I'd like to add a column to each to essentially label that set of data before merging all 8 .txt files into a Pandas dataframe.
Input: text file 1 (files 2-8 have same format)
585978391360221184|Thu Apr 09 01:31:50 +0000 2015|Recipes for Summer http://bbc.in/1CimpJF
583616089340280832|Thu Apr 02 13:04:53 +0000 2015|Health highlights http://bbc.in/1EKlFCK
Desired output (with labels added at end)
585978391360221184|Thu Apr 09 01:31:50 +0000 2015|Recipes for Summer http://.in/1CimpJF|BBC
583616089340280832|Thu Apr 02 13:04:53 +0000 2015|Health highlights http://bbc.in/1EKlFCK|BBC
Once I have all those labels added to each text file, I believe I could simply merge the 8 files and read them into a Pandas DataFrame. What's the best way to add those labels to the data to do this easily in Python? Is there a way I can do it automatically on all 8 files without doing it manually on each one?
Upvotes: 0
Views: 737
Reputation: 7693
files = ['d.csv','d1.csv'] # List of files
for file in files:
df = pd.read_csv(file, sep='|', header=None) # read file
df['label']='BBC' # add new column
df.to_csv(file, header=None, sep='|', index=None) # store file
IIUC you want to write 'BBC' label in each files.
Upvotes: 2