在Jupyter Notebook中运行Python代码时,出现回溯错误,我不确定是什么原因导致了回溯错误。任何帮助将不胜感激。
import pandas as pd
import requests
import target
import pip_api
from bs4 import BeautifulSoup
res = requests.get("http://web.archive.org/web/20070826230746/http://www.bbmf.co.uk/july07.html")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
df = df[1]
df = df.rename(columns=df.iloc[0])
df = df.iloc[2:]
df.head(15)
Southport = df[
(
df['Location'].str.contains('- Display') &
df['Lancaster'] == '' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'S' &
df['Hurricane'] == 'H'
)
] | df[
(
df['Location'].str.contains('- Display') &
df['Lancaster'] == '' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'S' &
df['Hurricane'] == ''
)
] | df[
(
df['Location'].str.contains('- Display') &
df['Lancaster'] == '' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'SS' &
df['Hurricane'] == ''
)
]
这是回溯错误的一部分,有什么想法吗? :-
TypeError Traceback (most recent call last)
<ipython-input-1-5ec7c798b521> in <module>
38 df['Hurricane'] == ''
39 )
---> 40 ] | df[
41 (
42 df['Location'].str.contains('- Display') &
c:\python37\lib\site-packages\pandas\core\ops.py in wrapper(self, other)
1848 filler = (fill_int if is_self_int_dtype and is_other_int_dtype
1849 else fill_bool)
-> 1850 res_values = na_op(self.values, ovalues)
1851 unfilled = self._constructor(res_values,
1852 index=self.index, name=res_name)
c:\python37\lib\site-packages\pandas\core\ops.py in na_op(x, y)
1808 "with a scalar of type [{typ}]"
1809 .format(dtype=x.dtype,
-> 1810 typ=type(y).__name__))
1811
1812 return result
答案 0 :(得分:0)
围绕各个比较,您需要更多的括号:
((df['Location'].str.contains('- Display')) &
(df['Lancaster'] == '') &
(df['Dakota'] == 'D') &
(df['Spitfire'] == 'S') &
(df['Hurricane'] == 'H'))
否则,&
在下一个等号==
之前与对象绑定,而不是下一个比较的结果。