I'm trying to load a large-ish (53MB) XML file into a pandas dataframe. Here are 3 rows of actual data (from public database of NTSB aviation accident reports), but the actual file has 77257 rows:
<?xml version="1.0"?>
<DATA xmlns="http://www.ntsb.gov">
<ROWS>
<ROW EventId="20150901X74304" InvestigationType="Accident" AccidentNumber="GAA15CA244" EventDate="09/01/2015" Location="Truckee, CA" Country="United States" Latitude="" Longitude="" AirportCode="" AirportName="" InjurySeverity="" AircraftDamage="" AircraftCategory="" RegistrationNumber="N786AB" Make="JOE SALOMONE" Model="SUPER CUB SQ2" AmateurBuilt="" NumberOfEngines="" EngineType="" FARDescription="" Schedule="" PurposeOfFlight="" AirCarrier="" TotalFatalInjuries="" TotalSeriousInjuries="" TotalMinorInjuries="" TotalUninjured="" WeatherCondition="" BroadPhaseOfFlight="" ReportStatus="Preliminary" PublicationDate=""/>
<ROW EventId="20150901X92332" InvestigationType="Accident" AccidentNumber="CEN15LA392" EventDate="08/31/2015" Location="Houston, TX" Country="United States" Latitude="29.809444" Longitude="-95.668889" AirportCode="IWS" AirportName="WEST HOUSTON" InjurySeverity="Non-Fatal" AircraftDamage="Substantial" AircraftCategory="Airplane" RegistrationNumber="N452CS" Make="CESSNA" Model="T240" AmateurBuilt="No" NumberOfEngines="" EngineType="" FARDescription="Part 91: General Aviation" Schedule="" PurposeOfFlight="Instructional" AirCarrier="" TotalFatalInjuries="" TotalSeriousInjuries="" TotalMinorInjuries="" TotalUninjured="2" WeatherCondition="VMC" BroadPhaseOfFlight="LANDING" ReportStatus="Preliminary" PublicationDate="09/04/2015"/>
<ROW EventId="20150729X33718" InvestigationType="Accident" AccidentNumber="CEN15FA325" EventDate="" Location="Truth or Consequences, NM" Country="United States" Latitude="33.250556" Longitude="-107.293611" AirportCode="TCS" AirportName="TRUTH OR CONSEQUENCES MUNI" InjurySeverity="Fatal(2)" AircraftDamage="Substantial" AircraftCategory="Airplane" RegistrationNumber="N32401" Make="PIPER" Model="PA-28-151" AmateurBuilt="No" NumberOfEngines="1" EngineType="Reciprocating" FARDescription="Part 91: General Aviation" Schedule="" PurposeOfFlight="Personal" AirCarrier="" TotalFatalInjuries="2" TotalSeriousInjuries="" TotalMinorInjuries="" TotalUninjured="" WeatherCondition="" BroadPhaseOfFlight="UNKNOWN" ReportStatus="Preliminary" PublicationDate="08/10/2015"/>
</ROWS>
</DATA>
The following code, which I adapted from here, works, but it is very slow for this data (more than 30 mins on my system). I can't seem to get the solution posted for the original example to work because the structure of my XML is different. Is there a more efficient way to load this data?
path_to_xml_file = mypath
import pandas as pd
import xml.etree.ElementTree as ET
#Load xml file data
tree = ET.parse(path_to_xml_file)
root = tree.getroot()
#Grab list of column names
aviationdata_column_names = root[0][0].attrib.keys()
#Create empty dataframe
aviationdata_df = pd.DataFrame(columns=aviationdata_column_names)
#Loop through tree and append to dataframe
for i in range(0,len(root[0])-1):
new_row = pd.Series(root[0][i].attrib)
new_row.name = i
aviationdata_df = aviationdata_df.append(new_row)
There are various solutions to similar questions posted around the internet (here, here, and here), but I haven't had luck implementing them. Version issues might be responsible for some of that (I am using Python 2.7).