python - 使用 pandas 从 xml 获取数据

标签 python pandas web service

我正在尝试使用 pandas 从 xml 获取一些数据。目前我有“工作”代码,我所说的工作是指它几乎可以工作。

import pandas as pd
import requests
from bs4 import BeautifulSoup

url = "http://degra.wi.pb.edu.pl/rozklady/webservices.php?"


response = requests.get(url).content
soup = BeautifulSoup(response)

tables = soup.find_all('tabela_rozklad')

tags = ['dzien', 'godz', 'ilosc', 'tyg', 'id_naucz', 'id_sala',
'id_prz', 'rodz', 'grupa', 'id_st', 'sem', 'id_spec']

df = pd.DataFrame()
for table in tables:
    all = map(lambda x: table.find(x).text, tags)
    df = df.append([all])

df.columns = tags

a = df[(df.sem == "1")]
a = a[(a.id_spec == "0")]
a = a[(a.dzien == "1")]
print(a)

所以我在“a = df[(df.sem == "1")]”上遇到错误,即:

文件“pandas\index.pyx”,第 139 行,位于 pandas.index.IndexEngine.get_loc (pandas\index.c:4443)

文件“pandas\index.pyx”,第 161 行,位于 pandas.index.IndexEngine.get_loc (pandas\index.c:4289)

文件“pandas\src\hashtable_class_helper.pxi”,第 732 行,位于 pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13733)

文件“pandas\src\hashtable_class_helper.pxi”,第 740 行,位于 pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13687)

当我阅读其他堆栈问题时,我看到人们建议使用 df.loc 所以我将此行修改为

a = df.loc[(df.sem == "1")]

现在代码已编译,但结果显示该行不存在。需要指出的是,问题仅出在“sem”标签上。休息完美,但不幸的是我需要使用这个标签。如果有人能解释我导致此错误的原因以及如何修复它,我将不胜感激。

最佳答案

您可以将 ignore_index=True 添加到 append为了避免重复的 index ,然后需要通过 [] 选择列 sem ,因为函数 sem :

df = pd.DataFrame()
for table in tables:
    all = map(lambda x: table.find(x).text, tags)
    df = df.append([all], ignore_index=True)

df.columns = tags
#print (df)

a = df[(df['sem'] == '1') & (df.id_spec == "0") & (df.dzien == "1")]
print(a)
    dzien godz ilosc tyg id_naucz id_sala id_prz rodz grupa id_st sem id_spec
0       1    1     2   0       52      79     13    W     1    13   1       0
1       1    3     2   0       12      79     32    W     1    13   1       0
2       1    5     2   0       52      65     13   Ćw     1    13   1       0
3       1   11     2   0      201       3     70   Ćw    10    13   1       0
4       1    5     2   0       36      78     13   Ps     5    13   1       0
5       1    5     2   1       18      32    450   Ps     3    13   1       0
6       1    5     2   2       18      32    450   Ps     4    13   1       0
7       1    7     2   1       18      32    450   Ps     7    13   1       0
8       1    7     2   2       18      32    450   Ps     8    13   1       0
9       1    7     2   0       66      65    104   Ćw     1    13   1       0
10      1    7     2   0      283       3    104   Ćw     5    13   1       0
11      1    7     2   0      346       5    104   Ćw     8    13   1       0
12      1    7     2   0      184      29     13   Ćw     7    13   1       0
13      1    9     2   0       66      65    104   Ćw     2    13   1       0
14      1    9     2   0      346       5     70   Ćw     8    13   1       0
15      1    9     1   0       73       3    203   Ćw     9    13   1       0
16      1   10     1   0       73       3    203   Ćw    10    13   1       0
17      1    9     2   0      184      19     13   Ps    13    13   1       0
18      1   11     2   0      184      19     13   Ps    14    13   1       0
19      1   11     2   0       44      65     13   Ćw     9    13   1       0
87      1    9     2   0      201      54    463    W     1    17   1       0
88      1    3     2   0       36      29     13   Ćw     2    17   1       0
89      1    3     2   0      211       5     70   Ćw     1    17   1       0
90      1    5     2   0      211       5     70   Ćw     2    17   1       0
91      1    7     2   0       36      78     13   Ps     4    17   1       0
105     1    1     2   1       11      16     32   Ps     2    18   1       0
106     1    1     2   2       11      16     32   Ps     3    18   1       0
107     1    3     2   0       51       3    457    W     1    18   1       0
110     1    5     2   2       11      16     32   Ps     1    18   1       0
111     1    7     2   0       91      64     97   Ćw     2    18   1       0
112     1    5     2   0      283       3    457   Ćw     2    18   1       0
254     1    5     1   0       12      29     32   Ćw     6    13   1       0
255     1    6     1   0       12      29     32   Ćw     5    13   1       0
462     1    7     2   0       98       1    486    W     1    19   1       0
463     1    9     1   0       91       1    484    W     1    19   1       0
487     1    5     2   0      116      19     13   Ps     1    17   1       0
488     1    7     2   0      116      19     13   Ps     2    17   1       0
498     1    5     2   0        0       0    431   Ps     2    17   1       0
502     1    5     2   0        0       0    431   Ps    15    13   1       0
503     1    5     2   0        0       0    431   Ps    16    13   1       0
504     1    5     2   0        0       0    431   Ps    19    13   1       0
505     1    5     2   0        0       0    431   Ps    20    13   1       0
531     1   13     2   0      350      79    493    W     1    13   1       0
532     1   13     2   0      350      79    493    W     2    17   1       0
533     1   13     2   0      350      79    493    W     1    18   1       0

关于python - 使用 pandas 从 xml 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41276407/

相关文章:

database - 什么时候发布数据库结构存在安全风险?

python - Output 类型的对象不可 JSON 序列化

python - 通过分组分隔列中的数据

python - 如何使用 Python 删除数据文件中的损坏事件?

python - 无法 reshape 每日时间序列的数据

javascript - 将数据保存到我的网站中的 firebase 后无法重定向到另一个 html 文件

python - 使用seaborn/matplotlib创建具有特定特征的条形图

python - 使用 loc 替换/选择列中的值。 Pandas

python - 使用 Pandas 代码创建独立文件

CSS Percent size specifier sizing element to more than specified size