I have the following data structure (original is 2.5gb, therefore parsing mandatory):
<households xmlns="http://www.matsim.org/files/dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.matsim.org/files/dtd http://www.matsim.org/files/dtd/households_v1.0.xsd">
<household id="1473">
<members>
<personId refId="2714"/>
<personId refId="2715"/>
<personId refId="2716"/>
<personId refId="2717"/>
<personId refId="2718"/>
<personId refId="2719"/>
</members>
<income currency="CHF" period="month">
3094.87101
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
<attribute name="carAvailability" class="java.lang.String" >some</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >3.3</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >3094.8710104279835</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >1</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >10213.074334412346</attribute>
</attributes>
</household>
<household id="2474">
<members>
<personId refId="4647"/>
<personId refId="4648"/>
<personId refId="4649"/>
<personId refId="4650"/>
<personId refId="4651"/>
<personId refId="4652"/>
<personId refId="4653"/>
<personId refId="4654"/>
<personId refId="4655"/>
</members>
<income currency="CHF" period="month">
1602.562822
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
<attribute name="carAvailability" class="java.lang.String" >all</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >3.6999999999999997</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >1602.5628215679633</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >1</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >5929.482439801463</attribute>
</attributes>
</household>
<household id="4024">
<members>
<personId refId="7685"/>
</members>
<income currency="CHF" period="month">
61610.096619
</income>
<attributes>
<attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
<attribute name="carAvailability" class="java.lang.String" >none</attribute>
<attribute name="consumptionUnits" class="java.lang.Double" >1.0</attribute>
<attribute name="householdIncomePerConsumptionUnit" class="java.lang.Double" >61610.096618936936</attribute>
<attribute name="numberOfCars" class="java.lang.Integer" >0</attribute>
<attribute name="residenceZoneCategory" class="java.lang.Integer" >1</attribute>
<attribute name="totalHouseholdIncome" class="java.lang.Double" >61610.096618936936</attribute>
</attributes>
</household>
</households>
I want to extract all the person ID refId
Values and their corresponding income
Value. Eventually, I plan to have a df with one column of the personId and one column with the income (income will be repetitive). So the tricky part is not only the namespace, but also how to access XML at different node levels.
My approach so far fails to do that.
import gzip
import xml.etree.ElementTree as ET
from collections import defaultdict
import pandas as pd
import numpy as np
tree = ET.parse(gzip.open('V0_1pm/output_households.xml.gz', 'r'))
root = tree.getroot()
rows = []
for it in root.iter('household'):
hh = it.attrib['id']
inc = it.find('income').text
rows.append([hh,inc])
hh_inc = pd.DataFrame(rows, columns=['id', 'PTSubscription'])
hh_inc
Any help is highly appreciated.