简体   繁体   中英

Another Traceback Error When I Run My Python Code

I have a new Traceback Error When, I run my Python Code. It appears to be to do with the very last ) Parentheses, also maybe the last ] in my Code.

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

And here is the Traceback Error I get :

        File "<ipython-input-5-6d53e7e5ec10>", line 31
        )
        ^
    SyntaxError: invalid syntax  

Here is my latest, whole Code John S, that works. I will let you know, if I get more issues, many thanks for your help :

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>

You just have to many brackets

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

You needed to remove a ')' after each ('- Display') it looks like you will still have some problems with sorting through your data. But this should get you past your syntax error.

Look at this online version so see my edits.

https://onlinegdb.com/Skceaucyr

you need to add ")]" in the end. So you variable southport will be now

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM