读取多个csv文件并将文件名添加为pandas中的新列 [英] Read multiple csv files and Add filename as new column in pandas

查看:226
本文介绍了读取多个csv文件并将文件名添加为pandas中的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中有几个csv文件,我想在一个数据帧中全部打开它们,并插入一个带有相关文件名的新列.到目前为止,我已经编写了以下代码:

I have several csv files in a single folder and I want to open them all in one dataframe and insert a new column with the associated filename. So far I've coded the following:

import pandas as pd
import glob, os
df = pd.concat(map(pd.read_csv, glob.glob(os.path.join('path/*.csv'))))
df['filename']= os.path.basename(csv)
df

这给了我想要的数据框,但是在新列文件名"中,它仅列出了文件夹中每一行的最后一个文件名.我正在寻找要填充与其相关的csv文件的每一行.不只是文件夹中的最后一个文件.

This gives me the dataframe I want but in the new column 'filename' it's only listing the last filename in the folder for every row. I'm looking for each row to be populated with it's associated csv file. Not just the last file in the folder.

对于这个新手的任何帮助,我们深表感谢.

Any assistance for this newbie is much appreciated.

推荐答案

我认为您需要

I think you need assign for add new column in loop, also parameter ignore_index=True was added to concat for remove duplicates in index:

要测试的文件为 a.csv b.csv


files = glob.glob('files/*.csv')
df = pd.concat([pd.read_csv(fp).assign(New=os.path.basename(fp).split('.')[0]) for fp in files])
print (df)
   a  b  c  d New
0  0  1  2  5   a
1  1  5  8  3   a
2  0  9  6  5   b
3  1  6  4  2   b
4  0  7  1  7   c
5  1  3  2  6   c

这篇关于读取多个csv文件并将文件名添加为pandas中的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆