Python Forum
Append files and add column with last part of each filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append files and add column with last part of each filename
#1
I hope one of you are willing to help a complete Python beginner. I have managed to create my first script where I append multiple excel files in a folder into one merged file. So far so good! But I also need the script to create an additional column and complete it with the last two characters of the filename from each file it appends.

My script looks like this for now:
import pandas as pd
import glob

# getting excel files to be merged
path = "C:\\Users\\123\\OneDrive\\Descriptions\\Translated"


# read all the files with extension .xlsx i.e. excel 
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)

# empty data frame for the new output excel file with the merged excel files
outputxlsx = pd.DataFrame()

# for loop to iterate all excel files
for file in filenames:
   # using concat for excel files
   # after reading them with read_excel()
   df = pd.concat(pd.read_excel(file, sheet_name=None), ignore_index=True, sort=False)

   # appending data of excel files
   outputxlsx = outputxlsx.append( df, ignore_index=True)

print('Final Excel sheet now generated at the same location:')
outputxlsx.to_excel("C:/Users/123/OneDrive/Descriptions/Translated/Merged.xlsx", index=False)
The files in the folder are named like this:

CZ, PL, TR_cs-CZ

CZ, PL, TR_pl-PL

CZ, PL, TR_tr-TR

So the last column should be:

CZ

PL

TR

Thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Two text files, want to add a column value zxcv101 8 1,947 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  Rename part of filename in multiple files atomxkai 7 7,377 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,509 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Append root directory folder and subdirectory to filename glittergirl 9 4,379 Aug-05-2020, 06:12 PM
Last Post: deanhystad
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,610 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  How to use a variable as part of a filename handyguy 10 4,582 Dec-28-2019, 11:49 PM
Last Post: handyguy
  Loop through folder of Excel Files and extract single column fioranosnake 2 4,542 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  Group files according to first few characters in filename python_newbie09 7 4,729 Aug-02-2019, 06:34 AM
Last Post: cvsae
  copy files from one destination to another by reading filename from csv Prince_Bhatia 3 7,655 Feb-27-2018, 10:56 AM
Last Post: Prince_Bhatia
  How to create def for sorted() from list of versioning files (filename+datetime) DrLove73 10 7,740 Jan-16-2017, 11:43 AM
Last Post: DrLove73

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020