Python 使用pandas从xml获取数据

Python 使用pandas从xml获取数据,python,pandas,web,service,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) ta

我试图使用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.IndexEngine.get_loc(pandas\index.c:4443)中第139行的文件“pandas\index.pyx”

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

pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:13733)中的第732行文件“pandas\src\hashtable_class_helper.pxi”

pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:13687)中的第740行文件“pandas\src\hashtable_class_helper.pxi”

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

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

现在编译代码,但结果显示这行代码不存在。需要指出的是,问题只与“sem”标签有关。Rest工作得很好,但不幸的是我需要使用这个标签。如果有人能解释我导致此错误的原因以及如何修复此错误,我将不胜感激。

您可以添加
忽略
,以避免重复
索引,然后需要通过
[]
选择列
sem
,因为函数:


这真的很好用,但是你能解释一下为什么我的代码没有用吗?谢谢你的接受。所以码字有问题,所以如果你的列是
sum
mean
count
sem
,你不能通过
df.sum
df.count
df.mean
df.sem
进行选择,因为它们都是函数。。。因此,对于该列,需要选择by
df['sum']
df['mean']
df['sem']
。请参阅文档-检查
警告
-
如果属性与现有方法名称冲突,例如不允许使用s.min,则该属性将不可用。
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