附加多个pandas DataFrmes从文件读入

时间:2018-06-02 02:53:37

标签: python pandas

您好我正在尝试读取多个文件,创建我需要的特定密钥信息的数据框,然后将每个文件的每个数据框附加到称为主题的主数据框。我试过以下代码。

import pandas as pd
import numpy as np
from lxml import etree
import os

topics = pd.DataFrame()
for filename in os.listdir('./topics'):
    if not filename.startswith('.'):
        #print(filename)
        tree = etree.parse('./topics/'+filename)
        root = tree.getroot() 
        childA = []
        elementT = []
        ElementA = []
        for child in root:
            elementT.append(str(child.tag))
            ElementA.append(str(child.attrib))
            childA.append(str(child.attrib))
            for element in child:
                elementT.append(str(element.tag))
                #childA.append(child.attrib)
                ElementA.append(str(element.attrib))
                childA.append(str(child.attrib))
                for sub in element:
                    #print('***', child.attrib , ':' , element.tag, ':' , element.attrib, '***')
                    #childA.append(child.attrib)
                    elementT.append(str(sub.tag))
                    ElementA.append(str(sub.attrib))
                    childA.append(str(child.attrib))

        df = pd.DataFrame()
        df['c'] = np.array (childA)
        df['t'] = np.array(ElementA)
        df['a'] = np.array(elementT)

        file = df['t'].str.extract(r'([A-Z][A-Z].*[words.xml])#')
        start = df['t'].str.extract(r'words([0-9]+)')
        stop = df['t'].str.extract(r'.*words([0-9]+)')
        tags = df['a'].str.extract(r'.*([topic]|[pointer]|[child])')
        rootTopic = df['c'].str.extract(r'rdhillon.(\d+)')
        df['f'] = file
        df['start'] = start
        df['stop'] = stop
        df['tags'] = tags
        # c= topic
        # r = pointerr
        # d= child
        df['topicID'] = rootTopic

        df = df.iloc[:,3:]
        topics.append(df)

然而,当我调用主题时,我得到以下输出

topics
Out[19]:_

有人可以让我知道我哪里出错了,也有任何改善我的杂乱代码的建议将不胜感激

1 个答案:

答案 0 :(得分:1)

与列表不同,当您追加到DataFrame时,会返回一个新对象。因此topics.append(df)会返回一个您永远不会存储在任何位置的对象,而topics仍然是您在第6行声明的空DataFrame。您可以通过

解决此问题
topics = topics.append(df)

但是,在循环中附加DataFrame是非常昂贵的练习。相反,您应该将每个DataFrame附加到循环中的列表中,并在循环后调用pd.concat()列表中的DataFrame

import pandas as pd

topics_list = []
for filename in os.listdir('./topics'):
    # All of your code
    topics_list.append(df) # Lists are modified with append

# After the loop one call to concat
topics = pd.concat(topics_list)