Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I , if possible, carry the end part of the code shown below, over onto another line(s) or alter the text, to achieve the desired outcome, with less code. I have typed the following code:

import pandas as pd
import requests
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[df['Location'].str.contains('- Display') & (df['Lancaster'] == '') & (df['Dakota'] == 'D') & (df['Spitfire'] == 'SS')] 
Southport

What I am trying to achieve is the following Data to be shown :- Displays only, and only showing Dakota Spitfire and Hurricane or Dakota and Spitfire or Dakota and Two Spitfires, if they are shown in the Data Table Schedule, here is the whole Code. It is the line starting Southport = that needs editing:

I get the following Traceback Error when I run the Code, which I believe is due to the line of code being too long:

File "<ipython-input-1-518a9f1c8e98>", line 23
    Southport
            ^
SyntaxError: invalid syntax

I am running the code in the internet program Jupyter Notebook

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
255 views
Welcome To Ask or Share your Answers For Others

1 Answer

In my opinion this is typical copy/paste error; you would simply have to delete the first (df[ after the first (df['Hurricane'] == 'H') | and the df[ after the second | - then there at least shouldn't be any syntax error anymore.

However, the logic is far too verbose, as either df['Location'].str.contains('- Display') and (df['Lancaster'] == '') and also (df['Dakota'] == 'D') is part of every or-separated boolean term.

Besides that, df['Location'].str.contains('- Display') & (df['Lancaster'] == '') & (df['Dakota'] == 'D') & (df['Spitfire'] == 'S') is a superset of df['Location'].str.contains('- Display') & (df['Lancaster'] == '') & (df['Dakota'] == 'D') & (df['Spitfire'] == 'S') & (df['Hurricane'] == 'H'), which means the latter doesn't provide more rows if you have the first anyway, so you can leave it completely away.
So everything is in a first step cut down to

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

which can be expressed shorter as

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

because A & B & C | A & B & D is A & B & (C | D).

And if I recall it right, a pattern like =='S' or =='SS' should be better expressed by pandas' isin:

Southport = df[df['Location'].str.contains('- Display') & (df['Lancaster'] == '') & (df['Dakota'] == 'D') & df['Spitfire'].isin(['S', 'SS'])] 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...