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

4

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

no need keep last line, we need only first 20 rows from text file
in that case you can preprocess the file beforehand to remove the last line and use the posted code snippet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.