1

I am trying to apply a function using IP addresses in a DataFrame. The idea is to look at all the IP's in the df['IP'] column and return the country to the df['Country'] column.

import geoip
import pandas as pd

def country_lookup(IP):
    return geoip.country(IP)

df = pd.DataFrame({'IP':['99.11.100.60','99.16.198.20','99.88.55.4'],
'Country':['na','na','na']})

Attempted Solution 1
df['Country'] = df['IP'].apply(country_lookup)  

Attempted Solution 2
for i in df['IP']:
    df['Country'] = country_lookup(i)

Both attempts returned same country code for all rows.

Thanks for the help I am new to programming and have not been able to find a solution.

2
  • 2
    For the future, please do not tag things as languages that they are not. This is clearly Python and not R. Commented Nov 1, 2016 at 19:14
  • you may want to check this question Commented Nov 1, 2016 at 21:22

1 Answer 1

1

Try with Lambda

df['Country'] = df['IP'].apply(lambda x: country_lookup(x))  
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help it is working for my example but I am running into an error in my program. IndexError: tuple index out of range any suggestion on what to look at. When printing my dataframe everything looks correct.
I would need the entire dataframe to check the problem. with the error only it's pretty hard to tell
I may have figured this one out. I am reading from an sql database and I don't have an index column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.