TypeError: to_append 应该是 Series 或 Series 的列表/元组,得到 DataFrame
Posted
技术标签:
【中文标题】TypeError: to_append 应该是 Series 或 Series 的列表/元组,得到 DataFrame【英文标题】:TypeError: to_append should be a Series or list/tuple of Series, got DataFrame 【发布时间】:2021-07-26 05:26:28 【问题描述】:代码在早些时候运行,然后就停止了。
我不明白为什么,如果有人可以帮助我对这个主题有所了解,那就太棒了。
import urllib.request
from bs4 import BeautifulSoup as bs
import re
import pandas as pd
import requests
locations = ['las-vegas-nv','denver-co']
for location in locations:
headers =
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
url = f'https://www.apartments.com/location/1-bedrooms/'
page = requests.get(url, headers=headers)
soup = bs(page.text, 'lxml')
table = soup.find("table", class_="rentTrendGrid")
rows = []
for tr in table.find_all('tr'):
rows.append([td.text for td in tr.find_all(['th', 'td'])])
#header_row = rows[0]
rows = list(zip(*rows[0:])) # tranpose the table
df = pd.DataFrame(rows[1:], columns=rows[0])
df['City'] = location
dfs.append(df)
df = pd.concat(dfs).reset_index(drop = True)
print(df)
【问题讨论】:
什么是dfs
?我没有看到它在任何地方定义。
我只是在搞乱代码...我知道它没有定义但是当 dfs 等于 df 时它仍然会抛出相同的错误
【参考方案1】:
原来我只需要从 for 循环中取出最后一部分
import urllib.request
from bs4 import BeautifulSoup as bs
import re
import pandas as pd
import requests
locations = ['las-vegas-nv','denver-co', 'lehi-ut']
dfs = []
for location in locations:
headers =
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
url = f'https://www.apartments.com/location/1-bedrooms/'
page = requests.get(url, headers=headers)
soup = bs(page.text, 'lxml')
table = soup.find("table", class_="rentTrendGrid")
rows = []
for tr in table.find_all('tr'):
rows.append([td.text for td in tr.find_all(['th', 'td'])])
#header_row = rows[0]
rows = list(zip(*rows[0:])) # tranpose the table
df = pd.DataFrame(rows[1:], columns=rows[0])
df['City'] = location
dfs.append(df)
df = pd.concat(dfs).reset_index(drop = True)
print(df)
【讨论】:
以上是关于TypeError: to_append 应该是 Series 或 Series 的列表/元组,得到 DataFrame的主要内容,如果未能解决你的问题,请参考以下文章
TypeError:层的输入应该是张量。得到:last_hidden_state
PyTorch - 获取 'TypeError: pic 应该是 PIL Image 或 ndarray。得到 <class 'numpy.ndarray'>' 错误
Python Typeerror:所有中间步骤都应该是转换器并实现拟合和转换
我应该如何处理“TypeError:store.addListener 不是函数”错误
TypeError:mention.send 不是函数 - discord.js
Angular Unit Test 应该创建 FAILED TypeError: Cannot read properties of undefined (reading 'preferences')