【发布时间】:2019-11-04 10:50:56
【问题描述】:
我有一个新的 Traceback 错误 当我运行我的 Python 代码时。它似乎与我的代码中最后一个 ) 括号有关,也可能是最后一个 ] 。
((df['Location'].str.contains('- Display')) &
df['Lancaster'] != 'L' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'SS' &
df['Hurricane'] != 'H'))
)]
这是我得到的 Traceback 错误:
File "<ipython-input-5-6d53e7e5ec10>", line 31
)
^
SyntaxError: invalid syntax
这是我最新的完整代码 John S,它有效。我会告诉你,如果我得到 更多问题,非常感谢您的帮助:
import pandas as pd
import requests
from bs4 import BeautifulSoup
res = requests.get("http://web.archive.org/web/20070701133815/http://www.bbmf.co.uk/june07.html")
汤 = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
df = df[1]
df = df.rename(列=df.iloc[0])
df = df.iloc[2:]
df.head(15)
display = df[(df['Location'].str.contains('- Display')) & (df['Dakota'].str.contains('D')) & (df['Spitfire'].str .contains('S')) & (df['Lancaster'] != 'L')]
显示
【问题讨论】:
-
你(至少)错过了一个结束
]。如果您仍然遇到语法错误,我建议您将其打印出来并(手动)识别每对(),[],{}。最后你应该找出哪些是不匹配的。 -
Thankyou SyntaxVoid,有没有 Python 实用程序,可以找到任何不匹配的地方?
标签: python python-3.x pandas jupyter-notebook