Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading a multiple .txt files in to python as dataframe

I was struggling to load multiple .txt files in to python that are in my desktop. I am totally new to Python. My goal is to load multiple .txt files, which is saved in the same directory. The .txt files are plain texts. Thanks in advance for your help!

like image 590
aufd34 Avatar asked Oct 19 '19 19:10

aufd34


People also ask

How do I import multiple text files into python?

Import the OS module in your notebook. Define a path where the text files are located in your system. Create a list of files and iterate over to find if they all are having the correct extension or not. Read the files using the defined function in the module.

How do I read a text file into a DataFrame in Python?

Method 2: Using read_table() We can read data from a text file using read_table() in pandas. This function reads a general delimited file to a DataFrame object. This function is essentially the same as the read_csv() function but with the delimiter = '\t', instead of a comma by default.

How do I create a DataFrame from multiple files?

We would ideally like to read in the data from multiple files into a single pandas DataFrame for use in subsequent steps. The most straightforward way to do it is to read in the data from each of those files into separate DataFrames and then concatenate them suitably into a single large DataFrame.


1 Answers

You could do something like this.


from collections import defaultdict
from pathlib import Path
import pandas as df

my_dir_path = "/parh/to/folder"

results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
    with open(file, "r") as file_open:
        results["file_name"].append(file.name)
        results["text"].append(file_open.read())
df = pd.DataFrame(results)
like image 126
Florian Bernard Avatar answered Sep 21 '22 09:09

Florian Bernard