Posts: 218
Threads: 27
Joined: May 2018
Jun-19-2019, 07:26 PM
(This post was last modified: Jun-19-2019, 07:28 PM by eddywinch82.)
Thankyou so much, for all your help snippsat.
I will work, on the Months element of the code, you supplied in your last reply.
How can I if possible ? Carry the Code shown below, over onto another Line. I have typed the following Code,
So that the following Data is shown :- Displays only, and only showing Dakota Spitfire and Hurricane or Dakota and Spitfire or Dakota and Two Spitfires, here is the Code :-
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['Dakota'] == 'D') & (df['Spitfire'] == 'S') | df[df['Location'].str.contains('- Display') & (df['Dakota'] == 'D') & (df['Spitfire'] == 'SS')]
Southport I have the same "Invalid Syntax Error" occuring, I think this is due to the Line being very long. Can the same Code, be carried over, onto another line ? With the same Code I have typed ? How do I do this, if that is the cause of the Error ?
Eddie
Posts: 7,080
Threads: 122
Joined: Sep 2016
Jun-19-2019, 09:14 PM
(This post was last modified: Jun-19-2019, 09:14 PM by snippsat.)
That's way to long
Split it up you eg in step 1 assign dataframe to a variable,that's get used in step 2.
Then in step 2-3 and can continue to do stuff.
# Step 1
display = df[(df['Location'].str.contains('- Display')) & (df['Spitfire'].str.contains('S'))]
display ## look at result,comment out and countiune to step 2
# Step 2
#new_display = display[(display['Lancaster'].str.contains('L')) & (display['Hurricane'].str.contains('H'))]
#new_display ## look at result
# Step 3 eg clean up.
#display_clean = new_display.dropna(axis='columns')
#display_clean ## look at result
Posts: 218
Threads: 27
Joined: May 2018
Jun-20-2019, 01:24 PM
(This post was last modified: Jun-20-2019, 01:26 PM by eddywinch82.)
Hi snippsat,
Is there a way I can shorten the line :-
df[df['Location'].str.contains Also how do you type does not Contain ?
If I get that written shorter, the code will run I think. As When I removed the second df[df['Location'].str.contains('- Display') part of the code, and shortened another part, it ran. My Code is now :-
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['Dakota'] == 'D') & (df['Spitfire'] == 'S') | (df['Spitfire'] == 'SS')]
Southport Eddie
Posts: 7,080
Threads: 122
Joined: Sep 2016
Jun-20-2019, 03:10 PM
(This post was last modified: Jun-20-2019, 03:10 PM by snippsat.)
(Jun-20-2019, 01:24 PM)eddywinch82 Wrote: Also how do you type does not Contain ? ~ for not,on multiple condition expression.
!= for not equal on Serie
Selecting Subsets of Data in Pandas.
Quote:If I get that written shorter, the code will run I think. As When I removed the second df[df['Location'].str.contains('- Display') part of the code, and shortened another part, it ran. My Code is now :-
It's still way to long,the line length is 282 characters,hard to read and i get syntax error if test.
PEP-8 has advice for 79 characters,but most think that for short,so when starting to get over 100 characters think of split it up.
Posts: 218
Threads: 27
Joined: May 2018
Jun-21-2019, 08:44 PM
(This post was last modified: Jun-22-2019, 09:15 PM by eddywinch82.)
Hi snippsat,
I have had help with this Python Code on www.stackoverflow.com, In terms of the layout of the Code Line, that was very long, and some other ammendments.
My Code now looks like the Following :-
from bs4 import BeautifulSoup
import requests
from pandas import pd
res = request.urlopen("http://web.archive.org/web/20070826230746/http://www.bbmf.co.uk/july07.html")
soup = BeautifulSoup(res)
print(soup)
#Your work from here
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'] != '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'))
)
] But when I run the Code, I get the following Traceback Error :-
Error: KeyError Traceback (most recent call last)
c:\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2656 try:
-> 2657 return self._engine.get_loc(key)
2658 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Location'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-6-660b90cc0ca2> in <module>
32 df['Hurricane'] == ''))
33 )
---> 34 ] | df[
35 (
36 ((df['Location'].str.contains('- Display') &
c:\python37\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2925 if self.columns.nlevels > 1:
2926 return self._getitem_multilevel(key)
-> 2927 indexer = self.columns.get_loc(key)
2928 if is_integer(indexer):
2929 indexer = [indexer]
c:\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Location'
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
I am not sure what has gone wrong here, any ideas ?
Regards
Eddie
Posts: 7,080
Threads: 122
Joined: Sep 2016
I am not gone fix that,i still mean you should take in step.
It's like asking 10 question in one sentence
Posts: 218
Threads: 27
Joined: May 2018
What does this error mean ? :- Error: TypeError Traceback (most recent call last)
<ipython-input-20-80c674c582c8> in <module>
29 df['Hurricane'] != 'H'))
30 )
---> 31 ] | df[
32 (
33 ((df['Location'].str.contains('- Display') &
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
Posts: 218
Threads: 27
Joined: May 2018
Jun-23-2019, 01:14 PM
(This post was last modified: Jun-23-2019, 01:14 PM by eddywinch82.)
Hi snippsat,
Could you describe, how I should take it in steps ? Could you give me
a method, showing how I would go about this ?
Posts: 7,080
Threads: 122
Joined: Sep 2016
(Jun-23-2019, 01:14 PM)eddywinch82 Wrote: Could you describe, how I should take it in steps ? I have already showed you that in post#22.
Posts: 218
Threads: 27
Joined: May 2018
Jun-23-2019, 03:01 PM
(This post was last modified: Jun-23-2019, 03:02 PM by eddywinch82.)
Hi snippsat, Yes sorry forgot about that, getting decent results now, many thanks for your help.
How do you type something or something ? I.e. (df['Spitfire'].str.contains('S' | '') I know what I have typed there, won't work, but how would I type, what I am getting at there ?
Eddie
|