0

I Need to Create Data Frame Using Multiple text files, all the text files are in same directory

Text File Format, each text file contains data showed in image

Text file format

here I need to create DataFrame using these kind of multiple text files

  • Please post the text as text, not as an image. – Michael Hodel Aug 30 '22 at 11:43
  • 2
    check https://stackoverflow.com/a/26415986/17489380 for a similar question and answer – Marvo Aug 30 '22 at 11:46
  • You can load them in separate dataframe and use join, merge, or concat – user16181 Aug 30 '22 at 11:52
  • Does this answer your question? [Read multiple \*.txt files into Pandas Dataframe with filename as column header](https://stackoverflow.com/questions/26415906/read-multiple-txt-files-into-pandas-dataframe-with-filename-as-column-header) – Daniil Fajnberg Aug 30 '22 at 13:45

1 Answers1

0

If it is possible to remove the last line (Name: 45559, dtype: object) then you should be able to load txt file as a csv:

import pandas as pd
import os


txt_files_dir = '...'
files = os.listdir(txt_files_dir)
dfs_list = [pd.read_csv(file, sep='\s+') for file in files]
data_frame_result = pd.concat(dfs_list, axis=0, ignore_index=True)
Stager
  • 1
  • 3