python - 运行 Python 代码时的另一个回溯错误
问题描述
我有一个新的 Traceback 错误,当我运行我的 Python 代码时。它似乎与我的代码中最后一个 ) 括号有关,也可能是最后一个 ] 。
((df['Location'].str.contains('- Display')) &
df['Lancaster'] != 'L' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'SS' &
df['Hurricane'] != 'H'))
)]
这是我得到的回溯错误:
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")
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)
display = df[(df['Location'].str.contains('- Display')) & (df['Dakota'].str.contains('D')) & (df['Spitfire'].str.contains('S')) & (df['Lancaster'] != 'L')]
display </code>
解决方案
您需要在最后添加“)]”。所以你可变的南港将是现在
Southport = df[
(
((df['Location'].str.contains('- Display') &
df['Lancaster'] != 'L' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'S' &
df['Hurricane'] == 'H'))
)
] | df[
(
((df['Location'].str.contains('- Display') &
df['Lancaster'] != 'L' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'S' &
df['Hurricane'] != 'H'))
)
] | df[
(
((df['Location'].str.contains('- Display') &
df['Lancaster'] != 'L' &
df['Dakota'] == 'D' &
df['Spitfire'] == 'SS' &
df['Hurricane'] != 'H'))
)]
推荐阅读
- c++ - 我总是在使用 Visual Studio 2019 和 OpenCV 的链接器中遇到错误
- python-3.x - 需要帮助来弄清楚如何更新一些 python 字典列表中的 key.value
- php - 如何使用正则表达式或数组将输入字符串转换为所需(给定)格式?
- angular - NGXS | 路由返回旧数据后选择
- c# - 为什么这个错误不断出现?索引超出范围:Unity 2D/WebGL C#
- java - gradle 任务没有显示额外的 java 任务
- python - 如何在 pandas 数据框中的每一行文本中用空格填充所有标点符号?
- angular - 取消订阅在RXJS上不起作用,正确的使用方法是什么?
- android - Android Firebase - 从单选按钮获取值时出错
- android - 如何在gradle中排除androidx库的所有版本文件?