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.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
Revision30
  • 11
  • 2

1 Answers1

1

Try with Lambda

df['Country'] = df['IP'].apply(lambda x: country_lookup(x))  
Steven G
  • 16,244
  • 8
  • 53
  • 77
  • 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. – Revision30 Nov 01 '16 at 20:46
  • I would need the entire dataframe to check the problem. with the error only it's pretty hard to tell – Steven G Nov 01 '16 at 20:54
  • I may have figured this one out. I am reading from an sql database and I don't have an index column. – Revision30 Nov 01 '16 at 20:57