如何创建一个合并并返回数据帧的类,但是从另一个类继承?

时间:2016-10-24 17:22:39

标签: python python-2.7 pandas glob

我有两个类,我需要第一个类来获取包含多个文件的输入路径;并且基于文件类型,它继承了特定的质量,例如文件提供程序名称,组以及从提供程序名称派生的skiprow和footer值。然后我需要将这些值传递给合并类,以便它可以合并为一个巨型连接的DataFrame,然后可以导出到csv。

import os
import pandas as pd
import logging
import glob
from pandas import Series, DataFrame


class Provider(object):

    def __init__(self, path, group, type1_or_type2):
        self.group = group.lower()
        self.path = path
        self.type1_or_type2 = type1_or_type2
        self.skipfooter = 1
        if self.group == 'AXEL'.lower():
            self.skiprows = 3
        else:
            self.skiprows = 0

class Datamerge(Provider):

    def __init__(self, dataframe):
        self.dataframe = DataFrame


    @classmethod
    def massread(cls):
        """
        Select your path and index provider to merge the data into one large dataframe
        """
        allfiles = glob.glob(self.path +"/*.*")
        list_ = []
        for file_ in allfiles:
            df = pd.read_csv(file_, skiprows = self.skiprows, skipfooter = self.skipfooter, engine = 'python')
            list_.append(df)
            frame = pd.concat(list_)
        return cls(frame)

s = Provider('F:\Desktop/FNMRA','AXEL','type1')
s.frame.to_csv('F:\Desktop/test.csv')

出现的错误是Traceback (most recent call last): File "F:\Desktop\Python Scripts\massload.py", line 47, in <module> s.DataMerge AttributeError: 'Provider' object has no attribute 'DataMerge' [Finished in 3.1s with exit code 1]

0 个答案:

没有答案