Python Pandas添加文件名列CSV [英] Python Pandas add Filename Column CSV
本文介绍了Python Pandas添加文件名列CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的python代码在以下示例中正常运行.我的代码结合了CSV文件的目录并匹配了标头.但是,我想更进一步-如何添加一列附加所使用CSV文件名的列?
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 add a column that appends the filename of the CSV that was used?
import pandas as pd
import glob
globbed_files = glob.glob("*.csv") #creates a list of all csv files
data = [] # pd.concat takes a list of dataframes as an agrument
for csv in globbed_files:
frame = pd.read_csv(csv)
data.append(frame)
bigframe = pd.concat(data, ignore_index=True) #dont want pandas to try an align row indexes
bigframe.to_csv("Pandas_output2.csv")
推荐答案
这应该有效:
import os
for csv in globbed_files:
frame = pd.read_csv(csv)
frame['filename'] = os.path.basename(csv)
data.append(frame)
frame['filename']
创建一个名为filename
的新列,而os.path.basename()
将类似/a/d/c.txt
的路径转换为文件名c.txt
.
frame['filename']
creates a new column named filename
and os.path.basename()
turns a path like /a/d/c.txt
into the filename c.txt
.
这篇关于Python Pandas添加文件名列CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文