Home » excel » excel – multiple csv files in row using in Python

excel – multiple csv files in row using in Python

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am very new to excel/csv operation in python.

I have 1k+ .xlsx file. Each excel file have 2 sheets , sheet1.csv , sheet2.csv (sheet names are identical in all the .xlsx) . From all the excel file , I have to collect sheet1.csv and have to create new excel with all the sheets.

Sheet1.scv from one excel looks like below:

Name    Age     Gender  State   City
N1      22         M    Cal     ABC
N2      32         M    NC      Dur

sheet1.csv from another excel looks like below:

Name    Age     Gender  State   City
N3      26       F      KLM     PQR
N4      23       M      SC      STM

I am looking for output like below including an empty row between each sheet data:

Name    Age     Gender  State   City
N1      22       M      Cal     ABC
N2      32       M      NC      Dur

N3      26       F      KLM     PQR
N4      23       M      SC      STM

I tried this but not able to understand how to proceed ahead for the above work:

for excel in glob.glob(os.path.join(os.getcwd(), "*.xlsx")):
      df=pd.read_excel(excel,sheet_name=0) #sheet1.csv is the first sheet in all excel
      df.to_excel('Final.xlsx')

This code only keeps the last csv data of the excel file. Please help to fix it for the above problem statement.

How to&Answers:

Is it possible, but integers columns will be converted to floats:

dfs = []
for excel in glob.glob(os.path.join(os.getcwd(), "*.xlsx")):
      df=pd.read_excel(excel,sheet_name=0) #sheet1.csv is the first sheet in all excel
      #if really need empty row 
      df = df.append(pd.DataFrame([np.nan] * len(df.columns)))
      dfs.append(df)

df = pd.concat(dfs)
df.to_excel('Final.xlsx')

Answer:

You can do the following operation to concat all the excels in the given path.

allFiles = glob.glob(os.path.join(os.getcwd(), "*.xlsx"))
frame = pd.DataFrame()
list_ = []
append = list_.append
for file_ in allFiles:
    df = pd.read_excel(file_,sheet_name=0)
    append(df)
frame = pd.concat(list_)