Mike
Mike

Reputation: 57

writing pandas data frame into xml

I want to write my pandas data frame into xml . My dataframe looks like this

Col1 Col2  Value 
a     b    5

c     d   6

My xml output should look like this

<Root>
    <Expense>
            <Col1>a</Col1>
            <Col2>b</Col2>
            <Value>5</Value>
    </Expense>
    <Expense>
            <Col1>c</Col1>
            <Col2>d</Col2>
            <Value> 5</Value>
    </Expense>
</Root>

Upvotes: 2

Views: 318

Answers (1)

Louis-Justin Tallot
Louis-Justin Tallot

Reputation: 480

Use pandas.DataFrame.to_xml :

import pandas as pd
# suppose you have a pd.DataFrame my_df

# save the pd.DataFrame to my_df.xml in the same repository :
my_df.to_xml("my_df.xml")

If your pandas version does not support to_xml, you can convert it to JSON then use json2xml.

Upvotes: 2

Related Questions