1

How do I export multiple dataframes to a single excel, I'm not talking about merging or combining. I just want a specific line from multiple log files to be compiled to a single excel sheet. I already wrote a code but I am stuck:

import pandas as pd
import glob
import os
from openpyxl.workbook import Workbook


file_path = "C:/Users/HP/Desktop/Pandas/MISC/Log Source"
read_files = glob.glob(os.path.join(file_path,"*.log"))


for files in read_files:
    logs = pd.read_csv(files, header=None).loc[540:1060, :]

    print(LBS_logs)

    logs.to_excel("LBS.xlsx")

When I do this, I only get data from the first log.

Appreciate your recommendations. Thanks !

  • Does this answer your question? [Putting many python pandas dataframes to one excel worksheet](https://stackoverflow.com/questions/32957441/putting-many-python-pandas-dataframes-to-one-excel-worksheet) – Mayank Porwal May 28 '20 at 07:32

1 Answers1

0

You are saving logs, which is the variable in your for loop that changes on each iteration. What you want is to make a list of dataframes and combine them all, and then save that to excel.

file_path = "C:/Users/HP/Desktop/Pandas/MISC/Log Source"
read_files = glob.glob(os.path.join(file_path,"*.log"))

dfs = []
for file in read_files:
    log = pd.read_csv(file, header=None).loc[540:1060, :]
    dfs.append(log)

logs = pd.concat(logs)
logs.to_excel("LBS.xlsx")
Bertil Johannes Ipsen
  • 1,656
  • 1
  • 14
  • 27