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'])]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…