read_xml 不 return Python 中的全部数据

read_xml does not return the whole data in Python

我有这个链接 XML,它显然有两个包含顶级项目的多个记录集:“Tours”和“Candidats”。

您可以在 https://www.resultats-elections.interieur.gouv.fr/telechargements/PR2022/resultatsT1/011/077/077001.xml

找到 XML

我想把它变成一个Pandas数据框,这样我可以更高效、更方便地阅读。我像这样将 URL 传递给 read_xml

pandas.read_xml("https://www.resultats-elections.interieur.gouv.fr/telechargements/PR2022/resultatsT1/011/077/077001.xml")

哪个 returns 这个非常不完整的数据框。 XML 中的全部数据不会在此数据框中返回。问题:我怎样才能最好地将整个数据放入我可以使用的 Pandas 数据框中?

         Type   Annee  CodReg  CodReg3Car         LibReg  CodDpt  \
0  Présidentielle  2022.0     NaN         NaN           None     NaN   
1            None     NaN    11.0        11.0  Île-de-France    77.0   

   CodMinDpt  CodDpt3Car          LibDpt  Commune  
0        NaN         NaN            None      NaN  
1       77.0        77.0  Seine-et-Marne      NaN

仅供参考:我可以阅读此链接 XML 和 Excel,它 returns 应该包含 13 行和 33 列的完整数据框。 Excel 可能会简单地通过大量重复将所有内容弄平,但如果我能用 Python 做到这一点就好了。

无法找到在常规 pandas 中执行此操作的方法,但幸运的是 a package 可以做到这一点。你可以安装它:

pip install pandas_read_xml

之后,运行这将产生所需的数据帧:

import pandas_read_xml as pdx
df = pdx.read_xml('https://www.resultats-elections.interieur.gouv.fr/telechargements/PR2022/resultatsT1/011/077/077001.xml')
pdx.fully_flatten(df)

给你:

Election|Scrutin|Type   Election|Scrutin|Annee  Election|Departement|CodReg Election|Departement|CodReg3Car Election|Departement|LibReg Election|Departement|CodDpt Election|Departement|CodMinDpt  Election|Departement|CodDpt3Car Election|Departement|LibDpt Election|Departement|Commune|CodSubCom  ... Election|Departement|Commune|Tours|Tour|Mentions|Exprimes|Nombre    Election|Departement|Commune|Tours|Tour|Mentions|Exprimes|RapportInscrit    Election|Departement|Commune|Tours|Tour|Mentions|Exprimes|RapportVotant Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|NumPanneauCand Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|NomPsn Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|PrenomPsn  Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|CivilitePsn    Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|NbVoix Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|RapportExprime Election|Departement|Commune|Tours|Tour|Resultats|Candidats|Candidat|RapportInscrit
0   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   1   ARTHAUD Nathalie    Mme 2   0,27    0,22
1   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   2   ROUSSEL Fabien  M.  13  1,75    1,41
2   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   3   MACRON  Emmanuel    M.  206 27,69   22,39
3   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   4   LASSALLE    Jean    M.  15  2,02    1,63
4   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   5   LE PEN  Marine  Mme 162 21,77   17,61
5   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   6   ZEMMOUR Éric    M.  79  10,62   8,59
6   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   7   MÉLENCHON   Jean-Luc    M.  124 16,67   13,48
7   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   8   HIDALGO Anne    Mme 7   0,94    0,76
8   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   9   JADOT   Yannick M.  45  6,05    4,89
9   Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   10  PÉCRESSE    Valérie Mme 68  9,14    7,39
10  Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   11  POUTOU  Philippe    M.  6   0,81    0,65
11  Présidentielle  2022    11  011 Île-de-France   77  77  077 Seine-et-Marne  001 ... 744 80,87   97,64   12  DUPONT-AIGNAN   Nicolas M.  17  2,28    1,85