【问题标题】:Pandas: How to take values in a DataFrame column and put them all in the same rowPandas:如何在 DataFrame 列中获取值并将它们全部放在同一行中
【发布时间】:2019-06-25 21:42:54
【问题描述】:

我有以下使用 pandas 库保存到 excel 中的 DataFrame:

Report No.   Score      Specifications
26-013RN42  >=1000      WaterSense certified
26-013RN42  >=1000      Single-Flush HET
26-013RN42  >=1000      Floor Mounted
26-013RN42  >=1000      2 Piece Unit
26-013RN42  >=1000      Round
26-013RN42  >=1000      Standard
26-013RN42  >=1000      Gravity
26-013RN42  >=1000      Floor Outlet
26-013RN42  >=1000      Flapper size 3in
26-013RN42  >=1000      Rough-in: 10"
26-013RN42  >=1000      Insulated: No

如您所见,“报告编号”列和“Score”列的值相同,但“Specifications”列的值不同。

我希望将“规格”列下的所有值合并为一行,如下所示:

Report No.   Score      Specifications
26-013RN42    >=1000     WaterSense certified, Single-Flush HET, Floor Mounted, 2 Piece Unit, Round, Standard, Gravity, Floor Outlet, Flapper size 3in, Rough-in: 10", Insulated: No

编辑:

这是我的输入代码。这段代码的目的是访问一个网站,抓取数据并将其组织成一个表格。之前没有发布它,因为它有点乱,我知道有办法让它更有效率。如果您对如何改进代码有任何建议,请告诉我!

蟒蛇:

url2 = 'https://www.map-testing.com/map-search/?start=3&searchOptions=AllResults'
urlh2 = requests.get(url2)
info2 = urlh2.text

soup = BeautifulSoup(info2, 'html.parser')
toilets = soup.find_all('div', attrs= {'class' : 'search-result'})
testlist = []
datalist = []

for s in toilets[0].stripped_strings:
    datalist.append(s)
dict = {}
count = 0
for info in datalist[:9]:
    if count == 0:
        dict[info] = datalist[count + 1]
        count += 1
    elif (count % 2) == 1:
        count += 1
        continue
    elif (count % 2) == 0:
        dict[info] = datalist[count + 1]
        count += 1
specs = datalist[11:22]
dict['Specifications'] = specs
df = pd.DataFrame(dict)

【问题讨论】:

  • 能否请您发布您的输入数据以便轻松复制/粘贴?
  • df.groupby(['Report No.', 'Score'])['Specifications'].agg(', '.join)
  • 你需要的最终值可以通过''.join(df.Specifications)获取

标签: python python-3.x pandas web-scraping


【解决方案1】:

使用BeautifulSoup 抓取 html 网页数据。并使用pandas库将json数据转换为DataFrame。

from bs4 import BeautifulSoup
import requests
import pandas as pd

url2 = 'https://www.map-testing.com/map-search/?start=3&searchOptions=AllResults'
urlh2 = requests.get(url2)

soup = BeautifulSoup(urlh2.text, 'html.parser')
results = soup.find_all('div', attrs= {'class' : 'search-result'})

jsonData = []

for row_obj in results:
    data = {}
    row = row_obj.find("div")

    #scrape Manufacturer
    manufacturer = row.find("div", string="Manufacturer")
    data['Manufacturer']  = manufacturer.find_next('div').text.strip()

    # scrape Model Name
    modelName = row.find("div", string="Model Name")
    data['Model Name'] = modelName.find_next('div').text.strip()

    # scrape Model Number
    modelNumber = row.find("div", string="Model Number")
    data['Model Number'] = modelNumber.find_next('div').text.strip()

    # scrape MaP Report No.
    maPReportNo = row.find("div", string="MaP Report No.")
    data['MaP Report No.'] = maPReportNo.find_next('div').text.strip()

    # scrape MaP Flush Score
    maPFlushScore = row.find("div", string="MaP Flush Score")
    data['MaP Flush Score'] = maPFlushScore.find_next('div').text.strip()

    # scrape Specifications
    specifications = row.find_all("li")
    data['Specifications'] = ",".join(i.text.strip() for i in specifications)

    jsonData.append(data)

df = pd.DataFrame(jsonData)

【讨论】:

  • 非常感谢您在我做了一个小改动后提供的帮助。 “Map Flush Score”应该找到下一个“span”,因为“div”正在返回文本“Compare”而不是实际分数。否则,我认为将其格式化为模仿 json 是一个很好的举措。再次感谢!
  • @LucksDesperation15 你说得对,我忘了把 MaP Flush Score div 改成 span。
猜你喜欢
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2022-10-21
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
相关资源
最近更新 更多