将超过1000个文件逐行处理到pandas.DataFrame中

时间:2019-06-15 11:29:45

标签: python-3.7

我有超过1000个文件,具有1kHz数据,我想打开每个文件并将其解析为pandas.DataFrame

但是要做到这一点,我需要在每一行中都发布流程,从而导致流程变得极其缓慢。 我可以做些其他事情来加快这一过程吗?

在每个文件中,都有一些预定义的列,然后是一些包含数据的列。参见屏幕转储

Screendump of a .txt file

def __readentry__(self, df : pandas.DataFrame, file_t):
    collection = {}

    try:
        lines = file_t.readlines()

        for line in lines:
            line_items = line.split('\t');

            if (line == ''):
                return pandas.DataFrame()

            offset = self.header_count

            v1 = line_items[:self.header_count]
            v2 = line_items[self.header_count:]

            for index in range(1,len(v2)):
                ## Create a column name.
                key = ('Key:%s') % (str(index))

                if (not key in collection):
                    collection[key] = 0.0;

                collection[key] = (float)(v2[index])


            df_index = pandas.date_range(start=pandas.to_datetime(line_items[0]), periods=1)
            __df__ = pandas.DataFrame.from_records(collection, index=df_index)

            if not type(__df__) is pandas.DataFrame or __df__.empty:
                return


            df = df.append(__df__)
    except Exception as ex:
        print(ex)

0 个答案:

没有答案