country convert to continent

2024/11/9 4:50:59
def country_to_continent(country_name):country_alpha2 = pc.country_name_to_country_alpha2(country_name)country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)return country_continent_name
countries = list(df['country'])[country_to_continent(country)for country in countries] 
def country_to_continent(country_name):country_alpha2 = pc.country_name_to_country_alpha2(country_name)country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)return country_continent_namecountry_name = list(df['country'])
country_to_continent(country_name)

Acutually I can't get it why my second one is wrong but the first one is right . and get unhashable error

Answer

In the second snippet, you pass list of countries to country_to_continent function, which according to the first example, receives a single country as a parameter.

If you want to convert the whole column in your Dataframe, try instead:

print(df["country"].apply(lambda x: country_to_continent(x)))
https://en.xdnf.cn/q/120532.html

Related Q&A

Iterate through a list and delete certain elements

Im working on an assignment in my computer class, and Im having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code Im having issues with):You a…

How to count numbers in a list via certain rules?

Just to say I have a str and a list of strs and I want to count how many strs in the list that is contained in a str. Is there an elegant way to do that?For example, l = {"foo", "bar&qu…

Count character occurrences in a Python string

I want to get the each character count in a given sentence. I tried the below code and I got the count of every character but it displays the repeated characters count in the output. How to delete repe…

The Concept Behind itertoolss product Function

so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway.def product1(*args, **k…

how to read data from multiple sheets in a single csv file using python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Quality Center: Set a Step Field in Python

I have a very simple problem here. I want to achieve the following VB script Code in Python:- dim objSfact dim objOrun dim mystep Set objOrun = QCutil.CurrentRun Set objSfact = objOrun.StepFactory…

Convert for loop from Python to C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Storing values in a CSV file into a list in python

Id like to create a list that stores all values of a single column. For example lets say my file has a column called FirstNames and for the first 3 rows, the names column has Merry, Pippin, Frodo.Id l…

Python arithmetic quiz task 1

I have no idea why this code is not working, as you can see Im trying to ask the user 10 questions display their score at the end. Everything works except that the score will always appear as a 0 or 1 …

Using Python to split long string, by given ‘separators’ [duplicate]

This question already has answers here:Split Strings into words with multiple word boundary delimiters(31 answers)Closed 9 years ago.Environment: Win 7; Python 2.76I want to split a long string into pi…