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'])
添加回答
舉報
