Python Pandas add Filename Column CSV

前端 未结 2 424
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 09:42

My python code works correctly in the below example. My code combines a directory of CSV files and matches the headers. However, I want to take it a step further - how do I

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 10:41

    Mike's answer above works perfectly. In case any googlers run into the following error:

    >>> TypeError: cannot concatenate object of type ""; 
        only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
    

    It's possibly because the separator is not correct. I was using a custom csv file so the separator was ^. Becuase of that I needed to include the separator in the pd.read_csv call.

    import os
    
    for csv in globbed_files:
        frame = pd.read_csv(csv, sep='^')
        frame['filename'] = os.path.basename(csv)
        data.append(frame)
    

提交回复
热议问题