【问题标题】:Write to multiple csv using glob使用 glob 写入多个 csv
【发布时间】:2019-05-02 07:09:24
【问题描述】:

我正在处理大量 csv 文件,需要添加列。我试过glob,例如:

import glob

filenames = sorted(glob.glob('./DATA1/*2018*.csv'))
filenames = filenames[0:10]

import numpy as np
import pandas as pd

for f in filenames:
    df = pd.read_csv(f, header=None, index_col=None)
    df.columns = ['Date','Signal','Data','Code']
 #this is what I should add to all csv files   
    df["ID"] = df["Data"].str.slice(0,2) 

在将列添加到每个 csv 文件后,我需要一种方法将文件保存回具有不同名称的 csv(未连接),例如“file01edited.csv”。

【问题讨论】:

    标签: python pandas csv glob


    【解决方案1】:

    使用to_csvf-strings 更改文件名:

    for f in filenames:
        df = pd.read_csv(f, names=['Date','Signal','Data','Code'], index_col=None)
     #this is what I should add to all csv files   
        df["ID"] = df["Data"].str.slice(0,2) 
        #python 3.6+
        df.to_csv(f'{f[:-4]}edited.csv', index=False)
        #python bellow 3.6
        #df.to_csv('{}edited.csv'.format(f[:-4]), index=False)
    

    【讨论】:

    • 尝试了这些,但不断出现错误:长度不匹配:预期轴有 5 个元素,新值有 4 个元素。我可以知道 f[:-4] 代表什么吗?
    • @npm - 它从文件名中删除 .csv - file01.csvfile01 然后为 file01edited.csv 添加 edited.csv
    • @npm - 但您的错误意味着某个文件中有 5 列,因此 df.columns = ['Date','Signal','Data','Code']names=['Date','Signal','Data','Code'] 失败
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2021-02-02
    • 2019-02-12
    • 1970-01-01
    • 2021-04-24
    • 2011-08-18
    • 2014-08-03
    相关资源
    最近更新 更多