Is there a way to import several .txt files each becoming a separate dataframe using pandas?

I have to work with 50+ .txt files each containing 2 columns and 631 rows where I have to do different operations to each (sometimes with each other) before doing data analysis. I was hoping there was a way to import each text file under a different dataframe in pandas instead of doing it individually. The code I’ve been using individually has been

df = pd.read_table(file_name, skiprows=1, index_col=0) print(B)  

I use index_col=0 because the first row is the x-value. I use skiprows=1 because I have to drop the title which is the first row (and file name in folder) of each .txt file. I was thinking maybe I could use glob package and importing all as a single data frame from the folder and then splitting it into different dataframes while keeping the first column as the name of each variable? Is there a feasible way to import all of these files at once under different dataframes from a folder and storing them under the first column name? All .txt files would be data frames of 2 col x 631 rows not including the first title row. All values in the columns are integers.

Thank you

Add Comment
1 Answer(s)

Yes. If you store your file in a list named filelist (maybe using glob) you can use the following commands to read all files and store them on a dict.

dfdict = {f: pd.read_table(f,...) for f in filelist} 

Then you can use each data frame with dfdict["filename.txt"].

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.