У меня есть несколько xml с вложенными атрибутами
<Music>
<Groups>
<Artist>The Beatles</Artist>
<Releases>
<Release album="Abbey Road" year="1969" />
<Release album="The White Album" year="1968" />
</Releases>
</Groups>
<Groups>
<Artist>Bob Dylan</Artist>
<Releases>
<Release album="Blonde on Blonde" year="1966" />
<Release album="Blood on the Tracks" year="1975" />
</Releases>
</Groups>
<Groups>
<Artist>The Rolling Stones</Artist>
<Releases>
<Release album="Sticky Fingers" year="1971" />
<Release album="Exile On Main Street" year="1972" />
</Releases>
</Groups>
</Music>
Я пытаюсь вернуть шестистрочный фрейм данных, однако он создает отношения «многие ко многим», где каждый исполнитель назначается каждому альбому. Вот мой код и мой неверный результат:
import xml.etree.cElementTree as et
import pandas as pd
tree=et.parse(r'music.xml')
root=tree.getroot()
Artists=[]
AlbumTitle=[]
ReleaseYear=[]
for x in root.iter('Artist'):
root1=et.Element('root')
root1=x
for records in root.iter('Release'):
root2=et.Element('root')
root2=records
AlbumTitle.append(records.attrib['album'])
ReleaseYear.append(records.attrib['year'])
Artists.append(x.text)
df = pd.DataFrame({'Artists': Artists,
'AlbumTitle': AlbumTitle,
'ReleaseYear': ReleaseYear})
Current output:
Artists AlbumTitle ReleaseYear
------- ----------- -----
1 The Beatles Abbey Road 1969
2 The Beatles The White album 1968
3 The Beatles Blonde On Blonde 1966
4 The Beatles Blood on The tracks 1975
5 The Beatles Sticky Fingers 1971
6 The Beatles Exile On Main Street 1972
7 Bob Dylan Abbey Road 1969
8 Bob Dylan The White album 1968
... ... ...
18 The Rolling Stones Exile On Main Street 1972
Target output:
Artists AlbumTitle ReleaseYear
------- ----------- -----
1 The Beatles Abbey Road 1969
2 The Beatles The White album 1968
3 Bob Dylan Blonde On Blonde 1966
4 Bob Dylan Blood on The tracks 1975
5 The Rolling Stones Sticky Fingers 1971
6 The Rolling Stones Exile On Main Street 1972
Я прочитал документацию ElementTree, чтобы увидеть, как Artists.append может иметь строгую взаимосвязь при объединении с двумя атрибутами, но пока безуспешно. Любая помощь будет принята с благодарностью, спасибо