0

I've dataframe with name loaddata1 with column "IP_Adress" containing IP's in it and I want another column with name "city" which will display corresponding city based on Ip_adress. Can some suggest proper code. I've used below code but its not working. Also "IP_Adress" column is blank for some of record.


loaddata1 = pd.DataFrame({'ip': loaddata1['IP_Adress']})

loaddata1['city'] = loaddata1.loc[loaddata1['ip'].notna(), 'ip'].apply(lambda x: geocoder.ip(x).city) print(loaddata1['IP_Adress'], loaddata1['city']) `


Still getting Error enter image description here enter image description here

1 Answers1

0

You can use:

import geocoder

df = pd.DataFrame({'ip': ['151.101.1.69', '151.101.1.69']})

df['city'] = df['ip'].apply(lambda x: geocoder.ip(x).city)

For a faster run, you can combine it with pandarallel:

from pandarallel import pandarallel
import geocoder

pandarallel.initialize()

df = pd.DataFrame({'ip': ['151.101.1.69', '151.101.1.69']})

df['city'] = df['ip'].parallel_apply(lambda x: geocoder.ip(x).city)

output:

             ip           city
0  151.101.1.69  San Francisco
1  151.101.1.69  San Francisco
mozway
  • 194,879
  • 13
  • 39
  • 75
  • I've tried below code still giving me error. *** `from pandarallel import pandarallel` `import geocoder` `pandarallel.initialize()` `loaddata1 = pd.DataFrame({'ip': loaddata1['IP_Adress']})` `loaddata1['city'] = loaddata1['ip'].parallel_apply(lambda x: geocoder.ip(x).city)` *** – Asif Sayed Oct 24 '22 at 13:16
  • Please try first the **first code** (the `pandarallel` one is a bonus). Then provide the full error as edit to your question – mozway Oct 24 '22 at 13:21
  • updated above code with reference to your code still giving me error. Attached screenshot as well. – Asif Sayed Oct 24 '22 at 13:38
  • If you got this, you didn't get it from my code. Do you have NaNs in your data? You might want to apply the function to only the non-NA subset: `df['city'] = df.loc[df['ip'].notna(), 'ip'].parallel_apply(lambda x: geocoder.ip(x).city)` – mozway Oct 24 '22 at 13:40
  • Yes, I do have NaNs in IP_Adress column. Can you please suggest how to deal with that. – Asif Sayed Oct 24 '22 at 13:41
  • I just did in my previous comment. – mozway Oct 24 '22 at 13:41
  • please refer above code and screenshot of error. Please have a look and suggest. – Asif Sayed Oct 24 '22 at 13:53
  • I think the error is self-explanatory, you're performing too many requests to the online service are being blocked. You should then probably write a custom function to perform a limited number of request per second/minute. It feels to me out of the scope of this question which was on the syntax to be used. – mozway Oct 24 '22 at 13:55