【发布时间】:2024-05-02 21:25:02
【问题描述】:
我正在尝试在 python 脚本中将多个 csv 文件合并为 1 个 csv 文件。我想跳过编写每个 csv 文件的前 5 行。遇到一些麻烦,我是 Python 新手。我已经尝试了几个我发现的示例,但工作目录似乎有问题。这是我最近的尝试:
import pandas as pd
import csv
import glob
import os
path = '//server01/tmp/'
files_in_dir = [f for f in os.listdir(path) if f.endswith('csv')]
count = 0
for filenames in files_in_dir:
df = pd.read_csv(filenames)
if count < 6:
count += 1
continue
df.to_csv('out.csv', mode='a')
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
嗯,您想正确组合数据框。当您执行 pd.read_csv 时,它会读取整个块。所以计数不起作用。
-
如果您阅读整篇文章,
read_csv()是否有效,或者前 5 行是否破坏了它?如果是前者,您可以只读取 CSV 并在附加到主数据帧时省略这 5 行。
标签: python pandas csv concatenation cwd