【问题标题】:Load multiple csv files from a folder with a condition从具有条件的文件夹中加载多个 csv 文件
【发布时间】:2019-10-06 12:26:39
【问题描述】:

我有一个非常非结构化的文件夹,其中很多文件没有条目(只有行标题),但里面没有数据。我知道我可以包含它们,它们不会改变任何东西,但问题是标题在所有地方都不相同,所以每个文件都包含一些额外的手动工作。

到目前为止,我现在如何使用以下代码加载特定文件夹中的所有文件:

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)

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

【问题讨论】:

    标签: python pandas csv glob


    【解决方案1】:

    修改这个循环:

    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)
    

    收件人:

    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 语句的用途。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多