2017-11-22 2 views
1

私は一度に1つの病院を行うときに、私はイングランドのジョイントデータを廃止して、結果を正しいフォーマットにします。私は最終的にすべての病院で反復したいと思っていましたが、最初に3つの異なる病院の配列を作り、反復を理解することに決めました。私はちょうど1病院がある場合複数の再構成リストをパンダに追加する

以下のコードは私のパンダのデータフレームにおける最終結果の正しい形式を与える:私の反復バージョンで

import requests 
from bs4 import BeautifulSoup 
import pandas 
import numpy as np 
r=requests.get("http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile? 
hospitalName=Norfolk%20and%20Norwich%20Hospital") 
c=r.content 
soup=BeautifulSoup(c,"html.parser") 

all=soup.find_all(["div"],{"class":"toggle_container"})[1] 

i=0 
temp = [] 
for item in all.find_all("td"): 
    if i%4 ==0: 
     temp.append(soup.find_all("span")[4].text) 
     temp.append(soup.find_all("h5")[0].text) 
    temp.append(all.find_all("td")[i].text.replace(" ","")) 
    i=i+1 
table = np.array(temp).reshape(12,6) 
final = pandas.DataFrame(table) 
final 

を、私は、各結果セットを追加する方法を把握することはできません最終データフレームへ:

hosplist = ["http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Norfolk%20and%20Norwich%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Barnet%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Altnagelvin%20Area%20Hospital"] 
temp2 = [] 
df_final = pandas.DataFrame() 
for item in hosplist: 
    r=requests.get(item) 
    c=r.content 
    soup=BeautifulSoup(c,"html.parser") 

    all=soup.find_all(["div"],{"class":"toggle_container"})[1] 
    i=0 
    temp = [] 
    for item in all.find_all("td"): 
     if i%4 ==0: 
      temp.append(soup.find_all("span")[4].text) 
      temp.append(soup.find_all("h5")[0].text) 
     temp.append(all.find_all("td")[i].text) 
     i=i+1 
    table = np.array(temp).reshape((int(len(temp)/6)),6) 
    temp2.append(table) 
    #df_final = pandas.DataFrame(df) 

終わりには、「テーブルは、」私は必要なすべてのデータを持っているが、そのは、操作が容易ではないので、私は、データフレームに入れたいです。しかし、 "ValueError:2-d入力を渡す必要があります"というエラーが表示されます。

私はこのエラーは私が3次元にする3つの配列を持っていると言っていると思います。これはちょうど練習の反復ですが、私はデータフレームに入れる予定のデータを持っている400以上の病院がありますが、私は今ここで立ち往生しています。

+0

スタックトレースのpls。あなたは追加の代わりにextendを使ってみましたか? – skrubber

答えて

1

コードを少し再編成し、エンコードすることなくデータフレームを作成できました。

ソリューション:行がそれを引き起こしている間に見つける

hosplist = ["http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Norfolk%20and%20Norwich%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Barnet%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Altnagelvin%20Area%20Hospital"] 
temp = [] 
temp2 = [] 
df_final = pandas.DataFrame() 
for item in hosplist: 
    r=requests.get(item) 
    c=r.content 
    soup=BeautifulSoup(c,"html.parser") 

    all=soup.find_all(["div"],{"class":"toggle_container"})[1] 
    i=0 

    for item in all.find_all("td"): 
     if i%4 ==0: 
      temp.append(soup.find_all("span")[4].text) 
      temp.append(soup.find_all("h5")[0].text) 
     temp.append(all.find_all("td")[i].text.replace("-","NaN").replace("+","")) 
     i=i+1 
temp2.append(temp) 
table = np.array(temp2).reshape((int(len(temp2[0])/6)),6) 
df_final = pandas.DataFrame(table, columns=['h1', 'h2', 'h3', 'h4', 'h5', 'h6']) 
df_final 
+1

ニース!今はもっときれいです。 –

1

あなたの質問に対する簡単な答えはHEREです。

タフな部分はコードを取っていて、まだ正しくなかったものを見つけることでした。

フルコードを使用して、次のように変更しました。あなたと一緒にコピーしてください。

import requests 
from bs4 import BeautifulSoup 
import pandas 
import numpy as np 

hosplist = ["http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Norfolk%20and%20Norwich%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Barnet%20Hospital", 
      "http://www.njrsurgeonhospitalprofile.org.uk/HospitalProfile?hospitalName=Altnagelvin%20Area%20Hospital"] 
temp2 = [] 
df_final = pandas.DataFrame() 
for item in hosplist: 
    r=requests.get(item) 
    c=r.content 
    soup=BeautifulSoup(c,"html.parser") 

    all=soup.find_all(["div"],{"class":"toggle_container"})[1] 
    i=0 
    temp = [] 
    for item in all.find_all("td"): 
     if i%4 ==0: 
      temp.append(soup.find_all("span")[4].text) 
      temp.append(soup.find_all("h5")[0].text) 
     temp.append(all.find_all("td")[i].text) 
     i=i+1 
    table = np.array(temp).reshape((int(len(temp)/6)),6) 
    for array in table: 
     newArray = [] 
     for x in array: 
      try: 
       x = x.encode("ascii") 
      except: 
       x = 'cannot convert' 
      newArray.append(x) 
     temp2.append(newArray) 

df_final = pandas.DataFrame(temp2, columns=['h1', 'h2', 'h3', 'h4', 'h5', 'h6']) 
print df_final 

は、私は文字列がデータフレームに表示するために絶対に必要だったASCII変換のためのリストの内包表記を使用しようとしましたが、理解はエラーを投げていたので、私は例外で構築され、例外は決して示されません。

関連する問題