Python Enchant language descriptions - python

I'm working with the pyenchant module. I couldn't find in their documentaion and anywhere else, the descriptions for the languages listed in enchant.list_languages(). This function returns a list of tags, I need the human friendly descripction of each language.
import enchant
enchant.list_languages()
['af', 'am', 'ar', 'bg', 'bn', 'br', 'ca', 'cs', 'cy', 'da', 'de', 'de_AT', 'de_CH', 'de_DE', 'el', 'en', 'en_CA', 'en_GB', 'en_US', 'eo', 'es', 'et', 'eu', 'fa', 'fo', 'fr', 'fr_CH', 'fr_FR', 'ga', 'gl', 'gu', 'he', 'hi', 'hr', 'hsb', 'hu', 'hy', 'id', 'is', 'it', 'kk', 'kn', 'ku', 'lt', 'lv', 'ml', 'mr', 'nb', 'nl', 'nn', 'no', 'nr', 'ns', 'or', 'pa', 'pl', 'pt_BR', 'pt_PT', 'ro', 'ru', 'sk', 'sk_SK', 'sl', 'st', 'sv', 'ta', 'te', 'tl', 'tl_PH', 'tn', 'ts', 'uk', 'uz', 'xh', 'zu']
How can I translate all this tags to the human friendly name of these languages ('English', 'Spanish', 'Italian', etc)? Is there a function in pyenchant or another library to accomplish this?
Thanks a lot!!

You can use pycountry:
To get the language name, you can use:
pycountry.languages.get(alpha_2=language)
Where language is one of the pyenchant languages.
EDIT
Here is how you can print the pyenchant language names (and countries):
import pycountry
enchant_codes = [
"af", "am", "ar", "bg", "bn", "br", "ca", "cs", "cy", "da",
"de", "de_AT", "de_CH", "de_DE", "el", "en", "en_CA", "en_GB", "en_US", "eo",
"es", "et", "eu", "fa", "fo", "fr", "fr_CH", "fr_FR", "ga", "gl", "gu",
"he", "hi", "hr", "hsb", "hu", "hy", "id", "is", "it", "kk",
"kn", "ku", "lt", "lv", "ml", "mr", "nb", "nl", "nn", "no", "nr",
"ns", "or", "pa", "pl", "pt_BR", "pt_PT", "ro", "ru", "sk", "sk_SK", "sl",
"st", "sv", "ta", "te", "tl", "tl_PH", "tn", "ts", "uk", "uz", "xh", "zu",
]
for code in enchant_codes:
lang_code, _, country_code = code.partition("_")
if len(lang_code) == 2:
language = pycountry.languages.get(alpha_2=lang_code)
elif len(lang_code) == 3:
language = pycountry.languages.get(alpha_3=lang_code)
else:
language = None
language_name = language.name if language else "(unknown language)"
country = pycountry.countries.get(alpha_2=country_code) if country_code else None
country_name = country.name if country else ""
print(code, "=>", language_name, "/", country_name)

Related

Print frequency of the words along with word like (W1 : N1 ) (W2 : N2) with N1>N2 from a dictionary containing tuple in form of {(w1,n1),(w2,n2)}

I am trying to create a function that prints the word frequency after taking out the unnecessary words from the given texts and prints out : firs the word, then number of times it occurred in the text. So far I have been able to clean the text, count the words, and create a dictionary of the words with number of times its occurring in the text. Now I want to print it in a sorted format where word with highest count gets printed first, along with number of times its occurring. Here is the code I have been able to come up with. What should I do next ?
Here is a sample output of the dictionary :
{'welcome': 3, 'probe': 5, 'pm': 2, 'security': 6, 'breach': 2, 'propaganda': 4, 'sc': 2}
I want the output as
security : 6
probe : 5
Propaganda : 4
welcome : 3
enter code here
file = "/Users/learninguser/Documents/GitHub/Projects/News_analysis/news.txt"
f = open(file)
text = f.read()
def RemovePunchuations(text):
# removes all the special characters supplied in
# the variable punchuations from the supplied
# text string
newtext = text
punctuations = '''!()-[]{};:'"\,<>./?#—#$%^&*_~'''
for punchuation in punctuations:
newtext = newtext.replace(punchuation, " ")
return newtext
def TokenfromText(text):
# take a string as input
# return a list of words in the text
newtext = text
newnewtext = newtext.split()
return_list = []
for word in newnewtext:
newword = word.lower()
return_list.append(newword)
return return_list
def FrequencyWithUninterestingRemoved(listofwords):
# takes the tokens from text
# calculates frequency of words supplied in the list
# removes uninteresting words based on corpus supplied
# returns a dictionary of frequency
frequency = {}
supplied_list = listofwords
for word in supplied_list:
if word not in frequency:
frequency[word] = 1
frequency[word] += 1
uninteresting_words = ["the", "a", "to", "if", "is", "it", "of",
"and", "or", "an", "as", "i", "me", "my",
"we", "our", "ours", "you", "your", "yours",
"he", "she", "him", "his", "her", "hers",
"its", "they", "them", "their", "what",
"which", "who", "whom", "this", "that",
"am", "are", "was", "were", "be", "been",
"being", "have", "has", "had", "do",
"does", "did", "but", "at", "by", "with",
"from", "here", "when", "where", "how",
"all", "any", "both", "each", "few", "more",
"some", "such", "no", "nor", "too", "very",
"can", "will", "just", 'a', 'about', 'above',
'after', 'again', 'against', 'all', 'am',
'an', 'and', 'any', 'are', "aren't", 'as',
'at', 'be', 'because', 'been', 'before',
'didst', 'us', 'one',
'being', 'below', 'between', 'both',
'but', 'by', "can't", 'cannot', 'could',
"couldn't", 'did', "didn't", 'do', 'does',
"doesn't", 'doing', "don't", 'down', 'during',
'each', 'few', 'for', 'from', 'further', 'had',
"hadn't", 'has', "hasn't", 'have', "haven't",
'having',
'he', "he'd", "he'll", "he's", 'her', 'here', "here's",
'hers', 'herself', 'him', 'himself', 'his', 'how', "how's",
'i', "i'd", "i'll", "i'm", "i've", 'if', 'in', 'into', 'is',
"isn't", 'it', "it's", 'its', 'itself', "let's", 'me', 'more',
'most', "mustn't", 'my', 'myself', 'no', 'nor', 'not', 'of',
'off', 'on', 'once', 'only', 'or', 'other', 'ought', 'our',
'ours', 'ourselves', 'out', 'over', 'own', 'same', "shan't",
'she', "she'd", "she'll", "she's", 'should', "shouldn't", 'so',
'some', 'such', 'than', 'that', "that's", 'the', 'their', 'theirs',
'them', 'themselves', 'then', 'there', "there's", 'these', 'they',
"they'd", "they'll", "they're", "they've", 'this', 'those', 'through',
'to', 'too', 'under', 'until', 'up', 'very', 'was', "wasn't", 'we',
"we'd", "we'll", "we're", "we've", 'were', "weren't", 'what', "what's",
'when', "when's", 'where', "where's", 'which', 'while', 'who', "who's",
'whom', 'why', "why's", 'with', "won't", 'would', "wouldn't", 'you',
"you'd", "you'll", "you're", "you've", 'your', "thou", "thee", "thy",
"but", "man", 'yours', 'yourself', 'yourselves']
for uword in uninteresting_words:
if uword in frequency:
del frequency[uword]
return frequency
def Frequency_dict(text):
text1 = RemovePunchuations(text)
listofwords = TokenfromText(text1)
frequencyofwords = FrequencyWithUninterestingRemoved(listofwords)
return frequencyofwords
f_dict = Frequency_dict(text)
print(f_dict)
You can use pythons built in sorted function and then turn it back into a dict or just return the list of sorted tuples like so:
def Sorted_dict(text):
tempDict = Frequency_dict(text)
return dict(sorted(tempDict.items(), key=lambda kv: kv[-1], reverse=True))
f_dict = Sorted_dict(text)
Its turning the dict into a list of tuples with .items() then sorting using the value (kv[-1]) on each tuple. It uses reverse=True to make it go in decreasing order. Lastly, it converts that back into a dict and returns it.

pandas - Modify column values based on dictionary values

I have an CSV file containing a column "State" which contains US State names in full like: "New Jersey", "California", etc.
I want to modify this column so that they contain abbreviations instead of the full name like "NJ", "CA"...
To do this, I already have a dictionary that maps the state name to its abbreviation
us_state_abbrev = {
'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR', 'California': 'CA', 'Colorado': 'CO',
'Connecticut': 'CT', 'Delaware': 'DE', 'Florida': 'FL', 'Georgia': 'GA', 'Hawaii': 'HI', 'Idaho': 'ID',
'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA', 'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA',
'Maine': 'ME', 'Maryland': 'MD', 'Massachusetts': 'MA', 'Michigan': 'MI', 'Minnesota': 'MN', 'Mississippi': 'MS',
'Missouri': 'MO', 'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV', 'New Hampshire': 'NH', 'New Jersey': 'NJ',
'New Mexico': 'NM', 'New York': 'NY', 'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH', 'Oklahoma': 'OK',
'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI', 'South Carolina': 'SC', 'South Dakota': 'SD',
'Tennessee': 'TN', 'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT', 'Virginia': 'VA', 'Washington': 'WA',
'West Virginia': 'WV', 'Wisconsin': 'WI', 'Wyoming': 'WY'}
How do I loop through the column in my CSV file AND the dictionary and replace the full state name with the abbreviation?
Here's the code I wrote but it doesn't work:
with open(emp_file, 'r', errors='ignore') as fileHandle:
reader = csv.reader(fileHandle)
for row in reader:
for state, abbrev in us_state_abbrev.items():
if row[4] == state:
row[4] = abbrev
What am I doing wrong here? Please help.
import pandas as pd
df = pd.read_csv(emp_file)
then, assuming you know which column you want to edit:
df['State'] = df['State'].map(us_state_abbrev).fillna(df['State'])
Note: the last part deals with State entries not present in your dictionary

Passing a list of vars to a ridge.fit() -- unhashable type?

I have a dataset called training which was read in an manipulated using Pandas. There are about 150 variables, so I put them in a list and I want to pass them to a ridge regression; however, I get an error saying "unhashable type: list"
It's likely that I'm missing something obvious as this is my first pass using python (used to R and Stata).
Here's the code:
# Variables to use (potentially) -- for dummies, one has already been taken out to avoid dummy var trap
continuous_vars = ['VehicleAge','VehOdo', 'MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice', 'MMRAcquisitionRetailAveragePrice', 'MMRAcquisitonRetailCleanPrice', 'MMRCurrentAuctionAveragePrice', 'MMRCurrentAuctionCleanPrice', 'MMRCurrentRetailAveragePrice', 'MMRCurrentRetailCleanPrice', 'VehBCost', 'IsOnlineSale', 'WarrantyCost', 'reliability_score', 'num_bought']
make_cats = ['BUICK', 'CADILLAC', 'CHEVROLET', 'CHRYSLER', 'DODGE', 'FORD', 'GMC', 'HONDA', 'HUMMER', 'HYUNDAI', 'INFINITI', 'ISUZU', 'JEEP', 'KIA', 'LEXUS', 'LINCOLN', 'MAZDA', 'MERCURY', 'MINI', 'MITSUBISHI', 'NISSAN', 'OLDSMOBILE', 'PLYMOUTH', 'PONTIAC', 'SATURN', 'SCION', 'SUBARU', 'SUZUKI', 'TOYOTA', 'TOYOTA SCION', 'VOLKSWAGEN', 'VOLVO']
state_cats = ['AR', 'AZ', 'CA', 'CO', 'FL', 'GA', 'IA', 'ID', 'IL', 'IN', 'KY', 'LA', 'MA', 'MD', 'MI', 'MN', 'MO', 'MS', 'NC', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'SC', 'TN', 'TX', 'UT', 'VA', 'WA', 'WV']
auction_cats = ['ADESA', 'MANHEIM', 'OTHER']
trans_cats = ['AUTO']
color_cats = ['BEIGE', 'BLACK', 'BLUE', 'BROWN', 'GOLD', 'GREEN', 'GREY', 'MAROON', 'NOT AVAIL', 'ORANGE', 'OTHER', 'PURPLE', 'RED', 'SILVER', 'WHITE', 'YELLOW']
wheel_cats = ['Alloy', 'Covers', 'Special']
nat_cats = ['AMERICAN', 'OTHER', 'OTHER ASIAN', 'TOP LINE ASIAN']
size_cats =['COMPACT', 'CROSSOVER', 'LARGE', 'LARGE SUV', 'LARGE TRUCK', 'MEDIUM', 'MEDIUM SUV', 'SMALL SUV', 'SMALL TRUCK', 'SPECIALTY', 'SPORTS', 'VAN']
year_cats = ['2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010_x']
all_vars = continuous_vars + make_cats + state_cats + auction_cats + trans_cats + color_cats + wheel_cats + nat_cats + size_cats + year_cats
hashable = all_vars
## Ridge Regression
ridge_reg = Ridge(alpha=1)
ridge_reg.fit(training[hashable], training['IsBadBuy'])
Update
I've updated the code to reflect some suggestions. Here is the new error message:
File "C:\Anaconda\lib\site-packages\pandas\core\indexing.py", line 1068, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['reliability_score' 'num_bought' 'BUICK' 'CADILLAC' 'CHEVROLET' 'CHRYSLER'\n 'DODGE' 'FORD' 'GMC' 'HONDA' 'HUMMER' 'HYUNDAI' 'INFINITI' 'ISUZU' 'JEEP'\n 'KIA' 'LEXUS' 'LINCOLN' 'MAZDA' 'MERCURY' 'MINI' 'MITSUBISHI' 'NISSAN'\n 'OLDSMOBILE' 'PLYMOUTH' 'PONTIAC' 'SATURN' 'SCION' 'SUBARU' 'SUZUKI'\n 'TOYOTA' 'TOYOTA SCION' 'VOLKSWAGEN' 'VOLVO' 'AR' 'AZ' 'CA' 'CO' 'FL' 'GA'\n 'IA' 'ID' 'IL' 'IN' 'KY' 'LA' 'MA' 'MD' 'MI' 'MN' 'MO' 'MS' 'NC' 'NE' 'NH'\n 'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'SC' 'TN' 'TX' 'UT' 'VA' 'WA' 'WV'\n 'ADESA' 'MANHEIM' 'OTHER' 'AUTO' 'BEIGE' 'BLACK' 'BLUE' 'BROWN' 'GOLD'\n 'GREEN' 'GREY' 'MAROON' 'NOT AVAIL' 'ORANGE' 'OTHER' 'PURPLE' 'RED'\n 'SILVER' 'WHITE' 'YELLOW' 'Alloy' 'Covers' 'Special' 'AMERICAN' 'OTHER'\n 'OTHER ASIAN' 'TOP LINE ASIAN' 'COMPACT' 'CROSSOVER' 'LARGE' 'LARGE SUV'\n 'LARGE TRUCK' 'MEDIUM' 'MEDIUM SUV' 'SMALL SUV' 'SMALL TRUCK' 'SPECIALTY'\n 'SPORTS' 'VAN' '2001' '2002' '2003' '2004' '2005' '2006' '2007' '2008'\n '2009' '2010_x'] not in index"
What you did was create a list of lists, you want to concatenate them all:
all_vars = continuous_vars+ make_cats+ state_cats+ auction_cats+ trans_cats+ color_cats+ wheel_cats+ nat_cats+ size_cats+ year_cats
This will then select the feature columns from pandas correctly
Compare the following:
In [84]:
[auction_cats, wheel_cats]
Out[84]:
[['ADESA', 'MANHEIM', 'OTHER'], ['Alloy', 'Covers', 'Special']]
In [85]:
auction_cats+wheel_cats
Out[85]:
['ADESA', 'MANHEIM', 'OTHER', 'Alloy', 'Covers', 'Special']
I had this same exact problem a few days ago. Turns out you cannot use list type as keys in a dictionary. I got around this problem by assigning the list element to a new variable and used that variable as the key.
hashableVar = all_vars
training[hashableVar]

How do I convert a Django QuerySet into list of dicts?

How can I convert a Django QuerySet into a list of dicts? I haven't found an answer to this so I'm wondering if I'm missing some sort of common helper function that everyone uses.
Use the .values() method:
>>> Blog.objects.values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
>>> Blog.objects.values('id', 'name')
[{'id': 1, 'name': 'Beatles Blog'}]
Note: the result is a QuerySet which mostly behaves like a list, but isn't actually an instance of list. Use list(Blog.objects.values(…)) if you really need an instance of list.
The .values() method will return you a result of type ValuesQuerySet which is typically what you need in most cases.
But if you wish, you could turn ValuesQuerySet into a native Python list using Python list comprehension as illustrated in the example below.
result = Blog.objects.values() # return ValuesQuerySet object
list_result = [entry for entry in result] # converts ValuesQuerySet into Python list
return list_result
I find the above helps if you are writing unit tests and need to assert that the expected return value of a function matches the actual return value, in which case both expected_result and actual_result must be of the same type (e.g. dictionary).
actual_result = some_function()
expected_result = {
# dictionary content here ...
}
assert expected_result == actual_result
If you need native data types for some reason (e.g. JSON serialization) this is my quick 'n' dirty way to do it:
data = [{'id': blog.pk, 'name': blog.name} for blog in blogs]
As you can see building the dict inside the list is not really DRY so if somebody knows a better way ...
Type Cast to List
job_reports = JobReport.objects.filter(job_id=job_id, status=1).values('id', 'name')
json.dumps(list(job_reports))
You need DjangoJSONEncoder and list to make your Queryset to json, ref: Python JSON serialize a Decimal object
import json
from django.core.serializers.json import DjangoJSONEncoder
blog = Blog.objects.all().values()
json.dumps(list(blog), cls=DjangoJSONEncoder)
You do not exactly define what the dictionaries should look like, but most likely you are referring to QuerySet.values(). From the official django documentation:
Returns a ValuesQuerySet — a QuerySet subclass that returns
dictionaries when used as an iterable, rather than model-instance
objects.
Each of those dictionaries represents an object, with the keys
corresponding to the attribute names of model objects.
If you already have a query set you just use the list function to turn it into a list of dicts, eg:
list(MyModel.objects.values())
You can use the values() method on the dict you got from the Django model field you make the queries on and then you can easily access each field by a index value.
Call it like this -
myList = dictOfSomeData.values()
itemNumberThree = myList[2] #If there's a value in that index off course...
You could define a function using model_to_dict as follows:
from django.forms.models import model_to_dict
def queryset_to_list(qs,fields=None, exclude=None):
return [model_to_dict(x,fields,exclude) for x in qs]
Suppose your Model has the following fields
id
name
email
Run the following commands in Django shell
>>>qs=<yourmodel>.objects.all()
>>>list=queryset_to_list(qs)
>>>list
[{'id':1, 'name':'abc', 'email':'abc#ab.co'},{'id':2, 'name':'xyz', 'email':'xy#xy.co'}]
Say you want only the id and the name in the list of queryset dictionary
>>>qs=<yourmodel>.objects.all()
>>>list=queryset_to_list(qs,fields=['id','name'])
>>>list
[{'id':1, 'name':'abc'},{'id':2, 'name':'xyz'}]
Similarly, you can exclude fields in your output.
I found even a better solution:
This was my queryset:
queryset = TestDB.objects.values_list("country", "code")
Code above returned
<QuerySet [('Afghanistan', 'AF'), ('Albania', 'AL'), ('Algeria', 'DZ'), ('American Samoa', 'AS'), ('Andorra', 'AD'), ('Angola', 'AO'), ('Anguilla', 'AI'), ('Antarctica', 'AQ'), ('Antigua and Barbuda', 'AG'), ('Argentina', 'AR'), ('Armenia', 'AM'), ('Aruba', 'AW'), ('Australia', 'AU'), ('Austria', 'AT'), ('Azerbaijan', 'AZ'), ('Bahamas ', 'BS'), ('Bahrain', 'BH'), ('Bangladesh', 'BD'), ('Barbados', 'BB'), ('Belarus', 'BY'), '...(remaining elements truncated)...']>
and print(dict(queryset)) converted above into this:
{'Afghanistan': 'AF', 'Albania': 'AL', 'Algeria': 'DZ', 'American Samoa': 'AS', 'Andorra': 'AD', 'Angola': 'AO', 'Anguilla': 'AI', 'Antarctica': 'AQ', 'Antigua and Barbuda': 'AG', 'Argentina': 'AR', 'Armenia': 'AM', 'Aruba': 'AW', 'Australia': 'AU', 'Austria': 'AT', 'Azerbaijan': 'AZ', 'Bahamas ': 'BS', 'Bahrain': 'BH', 'Bangladesh': 'BD', 'Barbados': 'BB', 'Belarus': 'BY', 'Belgium': 'BE', 'Belize': 'BZ', 'Benin': 'BJ', 'Bermuda': 'BM', 'Bhutan': 'BT', 'Bolivia (Plurinational State of)': 'BO', 'Bonaire, Sint Eustatius and Saba': 'BQ', 'Bosnia and Herzegovina': 'BA', 'Botswana': 'BW', 'Bouvet Island': 'BV', 'Brazil': 'BR', 'British Indian Ocean Territory ': 'IO', 'Brunei Darussalam': 'BN', 'Bulgaria': 'BG', 'Burkina Faso': 'BF', 'Burundi': 'BI', 'Cabo Verde': 'CV', 'Cambodia': 'KH', 'Cameroon': 'CM', 'Canada': 'CA', 'Cayman Islands ': 'KY', 'Central African Republic ': 'CF', 'Chad': 'TD', 'Chile': 'CL', 'China': 'CN', 'Christmas Island': 'CX', 'Cocos (Keeling) Islands ': 'CC', 'Colombia': 'CO', 'Comoros ': 'KM', 'Congo (the Democratic Republic of the)': 'CD', 'Congo ': 'CG', 'Cook Islands ': 'CK', 'Costa Rica': 'CR', 'Croatia': 'HR', 'Cuba': 'CU', 'Curaçao': 'CW', 'Cyprus': 'CY', 'Czechia': 'CZ', 'Côte dIvoire': 'CI', 'Denmark': 'DK', 'Djibouti': 'DJ', 'Dominica': 'DM', 'Dominican Republic ': 'DO', 'Ecuador': 'EC', 'Egypt': 'EG', 'El Salvador': 'SV', 'Equatorial Guinea': 'GQ', 'Eritrea': 'ER', 'Estonia': 'EE', 'Eswatini': 'SZ', 'Ethiopia': 'ET', 'Falkland Islands [Malvinas]': 'FK', 'Faroe Islands ': 'FO', 'Fiji': 'FJ', 'Finland': 'FI', 'France': 'FR', 'French Guiana': 'GF', 'French Polynesia': 'PF', 'French Southern Territories ': 'TF', 'Gabon': 'GA', 'Gambia ': 'GM', 'Georgia': 'GE', 'Germany': 'DE', 'Ghana': 'GH', 'Gibraltar': 'GI', 'Greece': 'GR', 'Greenland': 'GL', 'Grenada': 'GD', 'Guadeloupe': 'GP', 'Guam': 'GU', 'Guatemala': 'GT', 'Guernsey': 'GG', 'Guinea': 'GN', 'Guinea-Bissau': 'GW', 'Guyana': 'GY', 'Haiti': 'HT', 'Heard Island and McDonald Islands': 'HM', 'Holy See ': 'VA', 'Honduras': 'HN', 'Hong Kong': 'HK', 'Hungary': 'HU', 'Iceland': 'IS', 'India': 'IN', 'Indonesia': 'ID', 'Iran (Islamic Republic of)': 'IR', 'Iraq': 'IQ', 'Ireland': 'IE', 'Isle of Man': 'IM', 'Israel': 'IL', 'Italy': 'IT', 'Jamaica': 'JM', 'Japan': 'JP', 'Jersey': 'JE', 'Jordan': 'JO', 'Kazakhstan': 'KZ', 'Kenya': 'KE', 'Kiribati': 'KI', 'Korea (the Democratic People Republic of)': 'KP', 'Korea (the Republic of)': 'KR', 'Kuwait': 'KW', 'Kyrgyzstan': 'KG', 'Lao People Democratic Republic ': 'LA', 'Latvia': 'LV', 'Lebanon': 'LB', 'Lesotho': 'LS', 'Liberia': 'LR', 'Libya': 'LY', 'Liechtenstein': 'LI', 'Lithuania': 'LT', 'Luxembourg': 'LU', 'Macao': 'MO', 'Madagascar': 'MG', 'Malawi': 'MW', 'Malaysia': 'MY', 'Maldives': 'MV', 'Mali': 'ML', 'Malta': 'MT', 'Marshall Islands ': 'MH', 'Martinique': 'MQ', 'Mauritania': 'MR', 'Mauritius': 'MU', 'Mayotte': 'YT', 'Mexico': 'MX', 'Micronesia (Federated States of)': 'FM', 'Moldova (the Republic of)': 'MD', 'Monaco': 'MC', 'Mongolia': 'MN', 'Montenegro': 'ME', 'Montserrat': 'MS', 'Morocco': 'MA', 'Mozambique': 'MZ', 'Myanmar': 'MM', 'Namibia': 'NA', 'Nauru': 'NR', 'Nepal': 'NP', 'Netherlands ': 'NL', 'New Caledonia': 'NC', 'New Zealand': 'NZ', 'Nicaragua': 'NI', 'Niger ': 'NE', 'Nigeria': 'NG', 'Niue': 'NU', 'Norfolk Island': 'NF', 'Northern Mariana Islands ': 'MP', 'Norway': 'NO', 'Oman': 'OM', 'Pakistan': 'PK', 'Palau': 'PW', 'Palestine, State of': 'PS', 'Panama': 'PA', 'Papua New Guinea': 'PG', 'Paraguay': 'PY', 'Peru': 'PE', 'Philippines ': 'PH', 'Pitcairn': 'PN', 'Poland': 'PL', 'Portugal': 'PT', 'Puerto Rico': 'PR', 'Qatar': 'QA', 'Republic of North Macedonia': 'MK', 'Romania': 'RO', 'Russian Federation ': 'RU', 'Rwanda': 'RW', 'Réunion': 'RE', 'Saint Barthélemy': 'BL', 'Saint Helena, Ascension and Tristan da Cunha': 'SH', 'Saint Kitts and Nevis': 'KN', 'Saint Lucia': 'LC', 'Saint Martin (French part)': 'MF', 'Saint Pierre and Miquelon': 'PM', 'Saint Vincent and the Grenadines': 'VC', 'Samoa': 'WS', 'San Marino': 'SM', 'Sao Tome and Principe': 'ST', 'Saudi Arabia': 'SA', 'Senegal': 'SN', 'Serbia': 'RS', 'Seychelles': 'SC', 'Sierra Leone': 'SL', 'Singapore': 'SG', 'Sint Maarten (Dutch part)': 'SX', 'Slovakia': 'SK', 'Slovenia': 'SI', 'Solomon Islands': 'SB', 'Somalia': 'SO', 'South Africa': 'ZA', 'South Georgia and the South Sandwich Islands': 'GS', 'South Sudan': 'SS', 'Spain': 'ES', 'Sri Lanka': 'LK', 'Sudan ': 'SD', 'Suriname': 'SR', 'Svalbard and Jan Mayen': 'SJ', 'Sweden': 'SE', 'Switzerland': 'CH', 'Syrian Arab Republic': 'SY', 'Taiwan (Province of China)': 'TW', 'Tajikistan': 'TJ', 'Tanzania, United Republic of': 'TZ', 'Thailand': 'TH', 'Timor-Leste': 'TL', 'Togo': 'TG', 'Tokelau': 'TK', 'Tonga': 'TO', 'Trinidad and Tobago': 'TT', 'Tunisia': 'TN', 'Turkey': 'TR', 'Turkmenistan': 'TM', 'Turks and Caicos Islands ': 'TC', 'Tuvalu': 'TV', 'Uganda': 'UG', 'Ukraine': 'UA', 'United Arab Emirates ': 'AE', 'United States Minor Outlying Islands ': 'UM', 'United States of America ': 'US', 'Uruguay': 'UY', 'Uzbekistan': 'UZ', 'Vanuatu': 'VU', 'Bolivarian Republic of Venezuela': 'VE', 'Viet Nam': 'VN', 'Virgin Islands (British)': 'VG', 'Virgin Islands (U.S.)': 'VI', 'Wallis and Futuna': 'WF', 'Western Sahara': 'EH', 'Yemen': 'YE', 'Zambia': 'ZM', 'Zimbabwe': 'ZW', 'Åland Islands': 'AX'}
Which I think you need in your case too (I guess).
values_list()¶
values_list(*fields, flat=False, named=False)
>>> Entry.objects.values_list('id', 'headline')
<QuerySet [(1, 'First entry'), ...]>
>>> from django.db.models.functions import Lower
>>> Entry.objects.values_list('id', Lower('headline'))
<QuerySet [(1, 'first entry'), ...]>
im a newbie in python and i love #David Wolever answer
user = Blog.objects.all()
user = list(user.values("username", "id"))
in my case i use this to print username
user = Blog.objects.all()
user = list(user.values("username"))
name = []
for i in user:
name.append(i["username"])
print(name)
# ["joe", "karen", "stuf"]
Simply put list(yourQuerySet).

Choropleth Map in python using plotly without State Codes.

I have the following code to plot a choropleth Map in python.
data = [dict(
type="choropleth",
autocolorscale= True,
locations = df[statename],
z = df[z].astype(float),
locationmode = 'USA-states',
text = df[state],
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
)),
colorbar = dict(title = title)
)]
layout = dict(title = title,
geo=dict(scope="usa",showlakes = True,lakecolor = 'rgb(255, 255, 255)'))
iplot(go.Figure(data=data,layout=layout),validate=False)
Is it possible to plot the map using just the State Names as input to locations or is it necessary to have the two Letter State Codes. When i use the state codes it works but when i use the state names it just gives me an empty map.
There might be a way to change the value passed to the argument locationmode to take state names instead of state codes. As an alternative if there's a dataframe statesdf with a column statesdf['state'] containing the state name you can use the below code to create state codes from state names:
state_codes = {
'District of Columbia' : 'dc','Mississippi': 'MS', 'Oklahoma': 'OK',
'Delaware': 'DE', 'Minnesota': 'MN', 'Illinois': 'IL', 'Arkansas': 'AR',
'New Mexico': 'NM', 'Indiana': 'IN', 'Maryland': 'MD', 'Louisiana': 'LA',
'Idaho': 'ID', 'Wyoming': 'WY', 'Tennessee': 'TN', 'Arizona': 'AZ',
'Iowa': 'IA', 'Michigan': 'MI', 'Kansas': 'KS', 'Utah': 'UT',
'Virginia': 'VA', 'Oregon': 'OR', 'Connecticut': 'CT', 'Montana': 'MT',
'California': 'CA', 'Massachusetts': 'MA', 'West Virginia': 'WV',
'South Carolina': 'SC', 'New Hampshire': 'NH', 'Wisconsin': 'WI',
'Vermont': 'VT', 'Georgia': 'GA', 'North Dakota': 'ND',
'Pennsylvania': 'PA', 'Florida': 'FL', 'Alaska': 'AK', 'Kentucky': 'KY',
'Hawaii': 'HI', 'Nebraska': 'NE', 'Missouri': 'MO', 'Ohio': 'OH',
'Alabama': 'AL', 'Rhode Island': 'RI', 'South Dakota': 'SD',
'Colorado': 'CO', 'New Jersey': 'NJ', 'Washington': 'WA',
'North Carolina': 'NC', 'New York': 'NY', 'Texas': 'TX',
'Nevada': 'NV', 'Maine': 'ME'}
statesdf['state_code'] = statesdf['state'].apply(lambda x : state_codes[x])

Categories