在pandas DataFram中追加多个经过整形的列表

2024-06-28 19:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

当我一次只做一家医院的时候,我正在废除英格兰的联合数据,并以我想要的格式得到结果。我最终想迭代所有的医院,但首先决定将三个不同的医院组成一个数组,并计算出迭代过程。在

当我只有一家医院时,下面的代码为我提供了熊猫数据框中最终结果的正确格式:

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

在我的迭代版本中,我无法找到将每个结果集附加到最终数据帧中的方法:

^{pr2}$

最后,“table”包含了我想要的所有数据,但是它不容易操作,所以我想把它放到一个DataFrame中。但是,我得到了一个“ValueError:Must pass 2d input”错误。在

我想这个错误是说我有3个数组,这将使它成为三维的。这只是一个实践迭代,有超过400家医院的数据我计划把他们的数据放入一个数据框,但我现在被困在这里了。在


Tags: 数据textimportpandas格式table数组all
2条回答

我对代码进行了一点重新组织,能够在不需要编码的情况下创建数据帧。在

解决方案:

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

你的问题的简单答案是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转换使用列表理解,这对于字符串在数据帧中显示是绝对必要的,但是这种理解引发了一个错误,因此我构建了一个异常,并且该异常从未显示。在

相关问题 更多 >