簡體   English   中英

運行 Python 代碼時的另一個回溯錯誤

[英]Another Traceback Error When I Run My Python Code

我有一個新的回溯錯誤,當我運行我的 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>

你只需要很多括號

((df['Location'].str.contains('- Display') & 
  df['Lancaster'] == '' & 
  df['Dakota'] == 'D' & 
  df['Spitfire'] == 'SS' & 
  df['Hurricane'] == ''))

您需要在每個 ('- Display') 之后刪除一個 ')' 看起來您在對數據進行排序時仍然會遇到一些問題。 但這應該可以讓您克服語法錯誤。

看看這個在線版本,看看我的編輯。

https://onlinegdb.com/Skceaucyr

您需要在最后添加“)]”。 所以你可變的南港現在將是

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'))
     )]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM