【发布时间】:2016-12-22 05:36:21
【问题描述】:
我正在尝试使用 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”标签上。休息完美但不幸的是我需要使用这个标签。如果有人能解释我导致此错误的原因以及如何解决它,我将不胜感激。
【问题讨论】: