问题描述
我有大约58个数据源,我正在尝试合并它们,但标题中收到错误。 我对此进行了大量研究,但仍然无法从google / stackoverflow等上的某些解决方案中受益。 我正在做的是删除每个文件的前5行,使用文件名作为新列中的引用,然后将它们组合为1个数据帧,以便可以从整体上查看数据。
# Sets Parameters
import os
directory = 'C:\Test\TG'
line_remove = 5
rename = 'File00'
extension = 'csv'
os.chdir(directory)
# Rename_files
paths = (os.path.join(root,filename)
for root,_,filenames in os.walk(directory)
for filename in filenames)
for path in paths:
# the '#' in the example below will be replaced by the '-' in the filenames in the directory
newname = path.replace(rename,'')
if newname != path:
os.rename(path,newname)
import glob
# Remove top lines
myfiles = glob.glob('*.csv')
for file in myfiles:
lines = open(file).readlines()
open(file,'w').writelines(lines[line_remove:])
# add columns
import glob
import os
import pandas as pd
myfiles = glob.glob('C:\Test\TG\*.csv')
for file in myfiles:
with open(file,'r') as f:
data = f.readlines()
data[0] = 'Count,Line,Date'+'\n'
with open(file,'w') as f:
for element in data:
f.write(element)
import pandas as pd
import glob
files = glob.glob('C:\BHPB\TG\*.csv')
df = pd.concat([pd.read_csv(fp).assign(Filename=os.path.basename(fp).split('.')[0]) for fp in files],engine ='python')
即使在使用 engine =“ python” 方法之后,尝试定义数据框时仍会出现以下错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)