Read multiple *.txt files into Pandas Dataframe with filename as column header

后端 未结 2 1686
太阳男子
太阳男子 2021-01-07 01:21

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 informati

2条回答
  •  梦毁少年i
    2021-01-07 02:10

    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.

提交回复
热议问题