Reputation: 25
I have a pandas dataframe with a single row. Now I need to
When I try to covert the dataframe to xml using 'to_xml()' function, I am not able to store this into a variable. Can anyone please help me with this?
I use azure synapse notebooks (python) to do this process.
Upvotes: 0
Views: 99
Reputation: 5317
Use the code below to convert a data frame into an XML string with a sample data frame:
import pandas as pd
data = {'Name': ['Alice'],
'Age': [25],
'City': ['New York']}
df = pd.DataFrame(data)
print(df)
xml_string = df.to_xml(index=False)
print(xml_string)
This will convert the data frame to an XML string, as shown below:
To add header tags to the XML string, modify it using the following code:
header = '<root>'
footer = '</root>'
modified_xml_string = header + xml_string + footer
print(modified_xml_string)
The modified XML string is displayed as shown below:
Upvotes: 0