Second Pair of eyes for Correctly writing out XML from DataFrame (Pandas)?

Saran Ahluwalia ahlusar.ahluwalia at gmail.com
Sun Mar 29 07:26:46 EDT 2015


I would appreciate feedback on whether I correctly exported my DataFrame and wrote into XML. I used the ElementTree library. My DataFrame has 11 rows and 8 columns (excluding the index column)

For your convenience here it is:


#My schema assumption:
#<list>
    #[<message>
        #<index>Some number row</index>
        #<date>Sample text </data>
    #</message>]
#</list>
        
document = ET.Element("list")

def make_message(document, row):
    msg = ET.SubElement(document, "message")
    for field in row.index:
        field_element = ET.SubElement(msg, field)
        field_element.text = row[field]
    return msg
    
def add_to_document(row):
    return make_message(document, row)

#df.apply(add_to_document, axis=0) ---> if I were to import a Dataframe stored in the variable
#"df", I would simply APPLY the add_to_document function and COMBINE this into a document 

ET.dump(document)

Thank you, in advance for your help. 



More information about the Python-list mailing list