Skip to content Skip to sidebar Skip to footer

Read Multiple *.txt Files Into Pandas Dataframe With Filename As Column Header

I am trying to import a set of *.txt files. I need to import the files into successive columns of a Pandas DataFrame in Python. Requirements and Background information: Each file

Solution 1:

You can read them into multiple dataframes and concat them together afterwards. Suppose you have two of those files, containing the data shown.

In [6]:
filelist = ['val1.txt', 'val2.txt']
print pd.concat([pd.read_csv(item, names=[item[:-4]]) for item in filelist], axis=1)
    val1  val2
01616154542   -314  -3143114151554461531537868684496464103733731133124344341331311493931553531687387317434318111119533533204646

Solution 2:

You're very close. ijk is the filename already, you don't need to access the list:

# Step 3: Build up DataFrame:
df = pd.DataFrame()
for ijk in filelist:
    frame = pd.read_csv(ijk)
    df = df.append(frame)
print df

In the future, please provide working code exactly as is. You import from pandas import * yet then refer to pandas as pd, implying the import import pandas as pd.

You also want to be careful with variable names. files is actually a single file path, and filelist and filesList have no discernible difference from the variable name. It also seems like a bad idea to keep personal documents in your python directory.

Post a Comment for "Read Multiple *.txt Files Into Pandas Dataframe With Filename As Column Header"