首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >read_xml不返回Python的全部数据。

read_xml不返回Python的全部数据。
EN

Stack Overflow用户
提问于 2022-04-18 08:41:59
回答 1查看 76关注 0票数 0

我有一个链接的XML,很明显,它有两个带有顶级项的多个记录集:“旅游”和“候选”。

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

我想把它变成一个潘达斯的数据,我可以更有效和方便地阅读。我将URL传递到read_xml中,如下所示:

代码语言:javascript
复制
pandas.read_xml("https://www.resultats-elections.interieur.gouv.fr/telechargements/PR2022/resultatsT1/011/077/077001.xml")

返回这个非常不完整的数据。XML中的整个数据不会在此数据中返回。问:我能做什么才能最好地将整个数据放到我可以使用的Pandas数据文件中呢?

代码语言:javascript
复制
         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

FYI:我可以用Excel读取这个链接的XML,它返回包含13行和33列的完整数据。Excel可能只是用大量的重复来平平所有的东西,但是如果我能用Python来做到这一点的话,那就好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-18 09:03:51

在普通熊猫身上找不到一种方法,但幸运的是,有一个包裹这样做。您可以通过以下方式安装它:

代码语言:javascript
复制
pip install pandas_read_xml

之后,运行它将得到所需的数据帧:

代码语言:javascript
复制
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)

给予你:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71909513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档