使用数据框groupby()
功能时遇到快速问题。我有一个BS4 html表被解析成Pandas数据帧,然后目的是为多个行嵌套一个' Field'使用Groupby()
。不知道为什么我得到了
' AttributeError:' list'对象没有属性' groupby''
错误。我的str(table)
不正确吗?
感谢。
import requests
from bs4 import BeautifulSoup
import json
from datetime import datetime
import pandas as pd
starttime = datetime.now()
#Agent detail to prevent scraping bot detection
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
header = {'User-Agent' : user_agent }
# Webpage connection
html = "http://factpages.npd.no/ReportServer?/FactPages/TableView/field
_production_monthly&rs:Command=Render&rc:Toolbar=false&rc:Parameters
=f&Top100=True&IpAddress=108.171.128.174&CultureCode=en"
r=requests.get(html, headers=header)
c=r.content
soup=BeautifulSoup(c,"html.parser")
table = soup.find('table', attrs={'class':'a133'})
#Pandas dataframe
df = pd.read_html(str(table))
j = (df.groupby(["Field (Discovery)","NPDID information carrier"],
as_index=False)
.apply(lambda x: x[[ 'Year','Month','Oil - saleable [mill Sm3]','Gas -
saleable [bill Sm3]','NGL - saleable [mill Sm3]','Condensate -
saleable [mill Sm3]','Oil equivalents - saleable [mill Sm3]','Water -
wellbores [mill Sm3]' ]].to_dict('r'))
.reset_index(drop=True)
.rename(columns={0: 'MonthlyProduction'})
.to_json(orient='records'))
print(j)
print(json.dumps(json.loads(j), indent=2, sort_keys=True))
答案 0 :(得分:0)
根据pandas文档https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_html.html pd.read_html
返回数据帧列表(每个表一个数据帧)。因此,您需要选择要分组的那个。像:
# for the first one:
df = pd.read_html(str(table))[0]