1 回答
TA贡献1895条经验 获得超3个赞
考虑分别构建temperature数据框和precipitation数据框,concat然后通过节点merge中的公共值将版本连接在一起。并考虑使用列表/字典理解将所有属性值绑定在一起。timelocation
import xml.etree.ElementTree as et
import pandas as pd
tree = et.parse('Input.xml') # load in the data
root = tree.getroot() # get the element tree root
temp_list = []; precip_list = []
for n, x in enumerate(root.iter('time')):
# GET LIST OF DICTIONARIES OF ALL ATTRIBUTES
x_list = [{i.tag+'_'+k:v for k,v in i.attrib.items()} for i in x.iter('*')]
# COMBINE INTO SINGLE DICTIONARY
x_dict = {k:v for d in x_list for k,v in d.items()}
# BUILD DATA FRAME
df = pd.DataFrame(x_dict, index=[0])
# SEPARATELY SAVE TO LIST OF DATA FRAMES
if 'temperature_unit' in df.columns: temp_list.append(df)
if 'precipitation_unit' in df.columns: precip_list.append(df)
# MERGE CONCATENATED SETS BY COMMON VARS
df = pd.merge(pd.concat(temp_list),
pd.concat(precip_list),
on=['time_to', 'time_datatype',
'location_altitude', 'location_latitude',
'location_longitude'],
suffixes=['_t','_p'])
添加回答
举报