I am new to Python. I have a text extract from a database and a csv wikipedia list of all countries , and I would like to check if the country is mentioned in the text and the number of times that it is mentioned. This is what I have done so far:
<code>
text = pd.read_sql(select_string, con)
#clean up
text = text.replace({'\n': ' '}, regex=True)
text = text.replace({'-': ' '}, regex=True)
text = text['ProductText']
print(text) #making sure it looks ok
country_codes = pd.read_csv('country-codes.csv')
codes = country_codes['English short name lower case']
count_occurrences=Counter(country for country in text if country in codes)
print(count_occurrences)
The problem is that the last piece of code is not picking up any countries at all so the output is Counter()
I suspect that the problem is with the loop but I am not sure how to fix it - any help would really be appreciated :)
What does Counter() look like? What does country_codes, and more specifically, codes, look like?
Also, you should probably rename some of your variables to... match what they actually are. Like this:
count_occurrences = Counter(word for word in text if word in codes)
Since we're assuming that not *every* word in the article is a country code.
Also, are you putting the whole text into lower/uppercase anywhere? If you're looking for "can", it wouldn't match "CAN" at all, for those poor Canadians :(
from collections import Counter
counter = Counter(iterable)
print(counter['item'])
How does this syntax highlighting works? :huh:
Counter() just returns Counter() in the console when the script is run. country_codes is the names of the csv file which is read into the script and codes is just a variable that I used to assign the relevant column in the csv file - country_codes['English short name lower case']
I am not matching on the Alpha-2 or Alpha-3 columns in the csv file which uses the 3 letter representation of the country like "CAN" XD
(Sep-21-2016, 09:05 PM)wavic Wrote: [ -> ]from collections import Counter
counter = Counter(iterable)
print(counter['item'])
How does this syntax highlighting works? :huh:
Use the python syntax highlighter, not the generic code one. (they're still working out the plugins)
Also, wouldn't your code just always give "0"?
>>> from collections import Counter
>>> cnt = Counter('Green eggs and spam')
>>> cnt['g']
2
>>> cnt['gg']
0
>>> cnt['eggs']
0
You may have to supply a bit more code, such as where and what have you defined "Counter". If you are getting an error, please include the Traceback. Never mind, still getting used to new forum :angel: :P
(Sep-21-2016, 09:05 PM)wavic Wrote: [ -> ]from collections import Counter
counter = Counter(iterable)
print(counter['item'])
How does this syntax highlighting works? :huh:
Use the Python icon sceditor,have upgraded name and color a little ;)
(Sep-21-2016, 09:12 PM)sparkz_alot Wrote: [ -> ]You may have to supply a bit more code, such as where and what have you defined "Counter". If you are getting an error, please include the Traceback. Never mind, still getting used to new forum :angel: :P
from collections import Counter
Counter(country for country in text if country in codes)
(Sep-21-2016, 09:12 PM)nilamo Wrote: [ -> ] (Sep-21-2016, 09:05 PM)wavic Wrote: [ -> ]from collections import Counter
counter = Counter(iterable)
print(counter['item'])
How does this syntax highlighting works? :huh:
Use the python syntax highlighter, not the generic code one. (they're still working out the plugins)
Also, wouldn't your code just always give "0"?
>>> from collections import Counter
>>> cnt = Counter('Green eggs and spam')
>>> cnt['g']
2
>>> cnt['gg']
0
>>> cnt['eggs']
0
My interpretation of this is that : count every word for word that is in the text if the word is also in the country code csv file - is this correct?