My name is Pablo, and this is my first question in this group. After checking others related posts, I´ve decided to make a request, I wonder if there is a way to perform the following.
Let´s suppose I´ve the following dataframe structure:
+----+---------+------------+------------+----------+
| | MRBTS | dest | gw | length |
|----+---------+------------+------------+----------|
| 0 | 13004 | 10.104.0.0 | 10.48.0.0 | 16 |
| 1 | 13004 | 10.107.0.0 | 10.45.0.0 | 16 |
| 2 | 13005 | 10.104.0.0 | 10.130.0.0 | 8 |
| 3 | 13005 | 10.102.0.0 | 10.130.0.0 | 8 |
| 4 | 13005 | 0.0.0.0 | 10.110.0.0 | 16 |
+----+---------+------------+------------+----------+
Test DF:
and I want to export into an XML list groupping by MRBTS like following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE raml SYSTEM 'raml20.dtd'>
<raml version="2.0" xmlns="raml20.xsd">
<cmData type="plan" scope="all" name="iprt" id="PlanConfiguration( 7152069 )">
<header>
<log dateTime="2020-06-19T07:38:16.000-03:00" action="created" appInfo="PlanExporter">InternalValues are used</log>
</header>
<managedObject distName="MRBTS-13004">
<list >
<item>
<p name="dest">10.104.0.0</p>
<p name="length">16</p>
<p name="gw">10.38.0.0</p>
</item>
<item>
<p name="dest">10.107.0.0</p>
<p name="length">16</p>
<p name="gw">10.45.0.0</p>
</item>
</list>
</managedObject>
<managedObject distName="MRBTS-13005">
<list >
<item>
<p name="dest">10.104.0.0</p>
<p name="length">8</p>
<p name="gw">10.130.8.0</p>
</item>
<item>
<p name="dest">10.102.0.0</p>
<p name="length">8</p>
<p name="gw">10.130.8.0</p>
</item>
<item>
<p name="dest">0.0.0.0</p>
<p name="length">16</p>
<p name="gw">10.110.0.0</p>
</item>
</list>
</managedObject>
</cmData>
</raml>
I get this code from another post (How do convert a pandas/dataframe to XML?), but I got stucked while trying to grouping by MRBTS:
import pandas as pd
df = pd.DataFrame({'MRBTS':['13004','13004','13005','13005','13005'],
'dest':['10.104.0.0','10.107.0.0','10.104.0.0','10.102.0.0','0.0.0.0'],
'gw':['10.48.0.0','10.45.0.0','10.130.0.0','10.130.0.0','10.110.0.0'],
'length':['16','16','8','8','16']})
def func(row):
xml = ['<list >']
for field in row.index:
xml.append(' <field name="{0}">{1}</field>'.format(field, row[field]))
xml.append('</list>')
return '\n'.join(xml)
print ('\n'.join(df.apply(func, axis=1)))
And this result:
<list >
<field name="MRBTS">13004</field>
<field name="dest">10.104.0.0</field>
<field name="gw">10.48.0.0</field>
<field name="length">16</field>
</list>
<list >
<field name="MRBTS">13004</field>
<field name="dest">10.107.0.0</field>
<field name="gw">10.45.0.0</field>
<field name="length">16</field>
</list>
<list >
<field name="MRBTS">13005</field>
<field name="dest">10.104.0.0</field>
<field name="gw">10.130.0.0</field>
<field name="length">8</field>
</list>
<list >
<field name="MRBTS">13005</field>
<field name="dest">10.102.0.0</field>
<field name="gw">10.130.0.0</field>
<field name="length">8</field>
</list>
<list >
<field name="MRBTS">13005</field>
<field name="dest">0.0.0.0</field>
<field name="gw">10.110.0.0</field>
<field name="length">16</field>
</list>
Could you help me with this issue?