Input to Pandas ( Reading to DataFrame ) | |
---|---|
read_csv | Reading data from csv file |
read_clipboard | Reading data from Clipboard and storing in csv file |
read_sql | Using MySql records to create Pandas DataFrame |
read_excel | Reading data from Excel file |
read_json | Reading data from Json file |
read_table | Reading Tab separated data from .tsv file |
read_sql_table | Reading from Database table |
read_html | Reading Tabular data from Web page |
read_pickle | Reading DataFrame object from pickled file |
Output from Pandas | |
to_clipboard | Data From DataFrame copied to clipboard |
to_csv | Saving data to CSV file |
to_dict | Saving data to Dictionary |
to_excel | Saving data to Excel file |
to_json | Saving / output data in Json format |
to_html | Saving DataFrame data as HTML table in file |
to_pickle | Saving DataFrame data by pickling in a file |
to_sql | Using DataFrame managing MySql database |
to_string | Saving DataFrame data to String or in a file |
insert | Add data to MySQL table from DataFrame |
import pandas as pd # For reading the Excel file
import xml.etree.ElementTree as ET # For creating and manipulating XML files
# Step 1: Load the Excel file
excel_file = "E:\\testing\\python_dtl.xlsx"
# Replace with the path to your Excel file
output_file = "E:\\python_dtl.xml"
# Replace with the desired output file name
data = pd.read_excel(excel_file) # Load the Excel file into a Pandas DataFrame
# Step 2: Create the root element for the XML file
root = ET.Element("root") # Root element for the XML file
# Step 3: Iterate through each row in the DataFrame and create XML elements
for _, row in data.iterrows():
# Create a parent element for each row
row_element = ET.SubElement(root, "row")
# Create child elements for each column
for col_name in data.columns:
col_element = ET.SubElement(row_element, col_name)
col_element.text = str(row[col_name]) # Add column value as text
# Step 4: Create an XML tree and save it to a file
tree = ET.ElementTree(root)
tree.write(output_file, encoding="utf-8", xml_declaration=True)
print(f"XML file '{output_file}' created successfully!")
import pandas as pd
import xml.etree.ElementTree as ET
# Step 1: Load the Excel file
# Replace with the path to your Excel file
excel_file = "E:\\testing\\python_dtl.xlsx"
output_file = "E:\\python_dtl.xml" # Replace with the desired output file name
data = pd.read_excel(excel_file) # Load the Excel file into a Pandas DataFrame
# Step 2: Create the root element for the XML file
root = ET.Element("root") # Root element for the XML file
# Step 3: Iterate through each row in the DataFrame and create XML elements
for _, row in data.iterrows():
# Create a parent element for each row
row_element = ET.SubElement(root, "row")
# Create child elements for each column
for col_name in data.columns:
col_element = ET.SubElement(row_element, col_name)
col_element.text = str(row[col_name]) # Add column value as text
# Step 4: Create an XML tree and save it to a file
tree = ET.ElementTree(root)
tree.write(output_file, encoding="utf-8", xml_declaration=True)
print(f"XML file '{output_file}' created successfully!")
The following for loop is a critical part of the code that processes the DataFrame and generates the XML structure:
for _, row in data.iterrows():
# Create a parent element for each row
row_element = ET.SubElement(root, "row")
# Create child elements for each column
for col_name in data.columns:
col_element = ET.SubElement(row_element, col_name)
col_element.text = str(row[col_name]) # Add column value as text
Example: If we print row for a DataFrame with the following data:
| Name | Age | City |
|-------|-----|--------------|
| John | 30 | New York |
| Alice | 25 | Los Angeles |
The output of print(row) during the first iteration will be:
Name John
Age 30
City New York
Name: 0, dtype: object
If the DataFrame contains the following data:
| Name | Age | City |
|-------|-----|--------------|
| John | 30 | New York |
| Alice | 25 | Los Angeles |
The loop will generate the following XML structure:
<row>
<Name>John</Name>
<Age>30</Age>
<City>New York</City>
</row>
<row>
<Name>Alice</Name>
<Age>25</Age>
<City>Los Angeles</City>
</row>
This loop dynamically converts each row of the DataFrame into XML, making it highly flexible for varying data structures.