2016-12-22 4 views
1

私はパンダを使用してxmlからデータを取得しようとしています。現在、私は "作業中の"コードを持っています。パンダを使用して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) 

だから私は "=のDF [(df.sem == "1")]" のエラーが取得しています:、

ファイル "パンダの\のindex.pyx"、行139 pandas.index.IndexEngine.get_loc(pandas \ index.c:4443)

pandas.index.IndexEngine.get_loc(pandas \ index.c:4289)のファイル "pandas \ index.pyx"

pandas.hashtable.PyObjectHashTable.get_item(パンダの\のhashtable.c:13733)でのファイル "パンダ\ SRC \ hashtable_class_helper.pxi"、行732、

pandas.hashtable.PyObjectHashTable.get_item(パンダの\のhashtable.c:13687)でのファイル "パンダ\ SRC \ hashtable_class_helper.pxi"、行740、

私は人々がDFを使用することをお勧め見て、他のスタックの質問を読んで.LOCので、私は今、コードのコンパイル

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

にこのラインをmodyfiedが、この行が存在しないように結果が表示されます。問題は "sem"タグだけであることに言及する必要があります。残りは完全に機能しますが、残念ながらこのタグを正確に使用する必要があります。誰かが私がこのエラーの原因とその解決方法を説明できるなら、私は感謝します。

答えて

1

あなたはindexの重複回避のためappendignore_index=Trueを追加し[]で列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 
+0

これは本当によく動作しますが、あなたは、なぜ私のコードを説明することができますされていない? – Wesspe

+0

受け付けていただきありがとうございます。だからコードワードに問題があります。あなたの列が 'sum'、' mean'、 'count'、' sem'の場合、 'df.sum'、' df.count'、 'df.mean'、 'df.sem'はすべてがpandas function [' sum'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.sum.html)、['mean' ](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html)...だから、この列では 'df ['sum']'、 'df [ 'mean'] '' ... 'df ['sem']'。 – jezrael

+0

docs [here](http://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access)を参照してください。 - 'warning'をチェックしてください - 既存のものと競合する場合、属性は利用できませんメソッド名、例s.minは許可されていません。 – jezrael

関連する問題