从具有条件的文件夹中加载多个CSV文件



我有一个非常非结构化的文件夹,其中很多文件没有条目(仅是行标头(,但是里面没有数据。我知道我可以包括它们,它们不会更改任何内容,但是问题是,每个文件都不相同,因此每个文件都包含一些额外的手动工作。

到现在为止

import glob
path = r'C:/Users/...'
all_files = glob.glob(path+ "/*.csv")
li = []
for filename in all_files:
    frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
    li.append(frame)
df = pd.concat(li, axis=0, ignore_index=True, sort=False)

如何跳过每个文件,只有一行?

从:

修改此循环
for filename in all_files:
    frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
    li.append(frame)

to:

for filename in all_files:
    frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
    if len(frame) > 1:
        li.append(frame)

这就是if语句的用途。

最新更新