我想读取多个文件,但是当我添加新的列年份时,给出错误消息:列表对象没有属性'Split'。我想要一样。参考:I would like to have the same as this one
文件名:衬衫_2016,衬衫_2017,鞋_2018,鞋子_2019,
所有文件名都使用item_year格式进行标准化。
import os
import glob
import pandas
import numpy
path='/Item/'
files = glob.glob(os.path.join(path,'.xlsx'))
data = []
for filename in files:
df = pd.read_excel(filename,header=None)
data.append(df)
df=pd.concat(data)
df['Year']= os.path.basename(files.split('.')[0],split['_'][1]))
正如AttributeError
所建议的那样,您要调用files
的变量split()
是一个列表,而不是字符串。
相反,请在实例化每个数据框时尝试设置年份:
for filename in files:
df = pd.read_excel(filename, header=None)
# Extract the year from the filename & save it
# on your dataframe before you concatenate the data.
year = filename.split('.')[0],split['_'][1]
df['Year'] = year
data.append(df)