蟒蛇。如何读取多个.csv文件?

时间:2017-06-21 18:53:04

标签: python csv pandas dataframe

我在.csv中有超过200个文件,我想在同一时间(当前和下一个)阅读和计算其中两个文件。我尝试使用 glob pandas数据框

import glob

for file in glob.glob(path+'*.csv'):
    x1 = pd.read_csv(file, delimiter=',', dtype=None, names=('x', 'y'))
    x2 = pd.read_csv(file + 1 , delimiter=',', dtype=None, names=('x', 'y'))

我没有其他想法。

2 个答案:

答案 0 :(得分:0)

如果您希望在每次迭代时使用当前和下一个文件,则应执行以下操作:

from glob import glob


files = glob('*.csv')

for i, file in enumerate(files[:-1]):
    x1 = pd.read_csv(file, delimiter=',', dtype=None, names=('x', 'y'))
    x2 = pd.read_csv(files[i+1] , delimiter=',', dtype=None, names=('x', 'y'))
    # Do what you want to do

这使用enumerate来跟踪files序列中当前文件的索引。这样,只需添加1当前索引,就可以在使用“当前”文件时获取“下一个文件”。

我希望这会有所帮助。

答案 1 :(得分:0)

您可以使用pairwise文档中的itertools食谱:

from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

来自:https://docs.python.org/3/library/itertools.html#itertools-recipes

和用法:

for file1, file2 in pairwise(glob.glob(path+'*.csv')):
    ...
相关问题