将所有csv文件合并到一个文件夹中,并使用Python中的部分文件名向dataframe添加一个新列

将所有csv文件合并到一个文件夹中,并使用Python中的部分文件名向dataframe添加一个新列,python,pandas,csv,Python,Pandas,Csv,我有一个文件夹里面有超过100个CSV文件。它们都有相同的前缀名。 如: shcool.Math001.csv School.Math002.csv。 School.Physics001.csv。等它们都包含相同数量的列 How can I merge all the CSV files in one data frame in Python and add a new column with those files names but the prefix name "School." nee

我有一个文件夹里面有超过100个CSV文件。它们都有相同的前缀名。 如: shcool.Math001.csv
School.Math002.csv。 School.Physics001.csv。等它们都包含相同数量的列

How can I merge all the CSV files in one data frame in Python and add a new column with those files names but the prefix name "School." needs to be removed?

I found some code example online but did not sovle my problem:

path = r'C:\\Users\\me\\data\\'              
all_files = glob.glob(os.path.join(path, "*"))

df_from_each_file = (pd.read_csv(f, sep='\t') for f in all_files)
concatdf = pd.concat(df_from_each_file, ignore_index=True)

尝试此项,但尚未测试:

导入操作系统
作为pd进口熊猫
路径=“”
dfs=[]
对于os.listdir(路径)中的文件名:
示例_df=pd.read_csv(文件名)
示例_df['filename']=''.join(文件名[7:])
dfs.append(示例_df)
df=pd.concat(dfs,axis=0,ignore_index=True)
添加新列的加载项生成器理解:

path = r'C:\\Users\\me\\data\\'              
all_files = glob.glob(os.path.join(path, "*"))

df_from_each_file = (pd.read_csv(f, sep='\t').assign(New=+os.path.basename(f[7:]).split('.')[0]) for f in all_files)
concatdf = pd.concat(df_from_each_file, ignore_index=True)
谢谢我得到错误代码:''error TOKENIZE data.C错误:在第4行,SAW2中应该有1个字段