Extract country name from text in column to create another column

newpy

I have tried different combinations to extract the country names from a column and create a new column with solely the countries. I can do it for selected rows i.e. df.address[9998] but not for the whole column.

import pycountry
Cntr = []
for country in pycountry.countries:
    for country.name in df.address:
        Cntr.append(country.name)

Any ideas what is going wrong here?

edit:

address is an object in the df and

df.address[:10] looks like this

       Address
0    Turin, Italy        
1    NaN                 
2    Zurich, Switzerland 
3    NaN                 
4    Glyfada, Greece     
5    Frosinone, Italy    
6    Dublin, Ireland     
7    NaN                 
8    Turin, Italy        
1    NaN                 
2    Zurich, Switzerland 
3    NaN                 
4    Glyfada, Greece     
5    Frosinone, Italy    
6    Dublin, Ireland     
7    NaN                 
8   ...                  
9    Kristiansand, Norway
Name: address, Length: 10, dtype: object

Based on Petar's response when I run individual queries I get the country correctly, but when I try to create a column with all the countries (or ranges like df.address[:5] I get an empty Cntr)

    import pycountry
    Cntr = []
    for country in pycountry.countries:
        if country.name in df['address'][1]:
            Cntr.append(country.name)
Cntr
Returns
[Italy]

and df.address[2] returns [ ] 
etc.

I have also run df['address'] = df['address'].astype('str')

to make sure that there are no floats or int in the column.

Kenan

Sample dataframe df = pd.DataFrame({'address': ['Turin, Italy', np.nan, 'Zurich, Switzerland', np.nan, 'Glyfada, greece']})

df[['city', 'country']] = df['address'].str.split(',', expand=True, n=2)

               address     city       country
0         Turin, Italy    Turin         Italy
1                  NaN      NaN           NaN
2  Zurich, Switzerland   Zurich   Switzerland
3                  NaN      NaN           NaN
4      Glyfada, greece  Glyfada        greece

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extracting Country name and year from column and create a new columns in dataframe

Extract data from a column to create another column in R

extract country names (or other entity) from column

Extract text from a column

Extract column name and match with another column

Isolate the country name from Location column

How to extract information from a dataframe name and create a column based on it

Extract a column name from a Pandas column index

Get country name from a range of IP number with google sheets script and paste the result in another column

Extract a number from text in a PowerBI field and create a new calculated column

Extract text of one column based on similar text from another column in r

Find max of rows from specific columns and extract column name and corresponding row value from another column

extract a column from text file

extract a column from a text file

extract of text from column and copy it into a new column

Unable to extract country name 'UK' from text using geograpy

Create a column grouping strings text extracted from a column based on another column in R

Create text files with name of the files from a column in a data frame in R

Extract text from column D to txt files and name files based on content of column C

Create a new column from another column in Python

Create column from a substring of another column

create and power a column from another column in R

Create a column from another column based on keywords

Extract value of a particular column name in pandas as listed in another column

How to extract a portion of a datframe column and create another column with that extraction

How to get the name of the country from the city and save in a column using r?

Get country name from dataframe column by comparing with a list

how to extract column name and type from MySQL?

google sheet : extract column Name from address