[英]IP2Location record to Pandas DataFrame
我已經使用 IP2Location 收集有關 IP 地址的信息,我希望它位於 DataFrame 中,但是當我嘗試使用pd.json_normalize(ip)
時出現錯誤。 AttributeError: 'IP2LocationRecord' object has no attribute 'values'
我從 IP2Location 得到的信息是這樣的格式,
{'ip': '66.249.79.244', 'country_short': 'US', 'country_long': 'United States of America', 'region': 'California', 'city': 'Mountain View', 'latitude': 37.405991, 'longitude': -122.078514, 'zipcode': '94043', 'timezone': '-08:00'}
我也嘗試過使用pd.DataFrame
但 df 中的結果是空的,只看到列名。
df = pd.DataFrame(ip, columns = ['ip','country_short','country_long','region','city','latitude','longitude','zipcode','timezone'])
預期結果
ip country_short country_long .... zipcode timezone
0 66.249.69.244 US United States of America 94043 -08:00
請注意錯誤:
AttributeError: 'IP2LocationRecord' object...
您正在嘗試做的是將IP2LocationRecord
類型的IP2LocationRecord
轉換為pandas.DataFrame
這是不可能的(您必須直接解壓縮所有字段的字典) 你在這里看到的:
{'ip': '66.249.79.244', 'country_short': 'US', 'country_long': 'United States of America', 'region': 'California', 'city': 'Mountain View', 'latitude': 37.405991, 'longitude': -122.078514, 'zipcode': '94043', 'timezone': '-08:00'}
實際上是repr(ip)
repr python3 參考(而不是字典)
Pandas 與IPinfo配合得非常好。 If you want to use Pandas to do analysis on IP Geolocation, I suggest, using IPinfo's Python module with the bulk/batch lookup method getBatchDetails
, even if you have a single IP address.
原因在於, getBatchDetails
返回一個字典字典,其中 IP 地址為鍵,地理位置信息為值。 這使得轉換為 Pandas dataframe 變得相當容易,並且列名被分配給 IP 地址。
例如:
# install with `pip install ipinfo`
import ipinfo
import pandas as pd
# initialize handler with access token
access_token = "insert_your_token_here"
handler = ipinfo.getHandler(access_token)
# declare the ip address list
# randomly generated IP addresses
ip_addresses = ['169.7.127.160', '90.130.144.160', '77.45.221.129', '27.167.41.249']
# do the IP address batch lookup
ip_data = handler.getBatchDetails(ip_addresses)
# convert it to a dataframe
df = pd.DataFrame(ip_data) # the default column names are the IP Addresses
# transposing the dataframe
df_trasposed = df.T # the IP Addresses will be the index column
當您使用getBatchDetails
查找 IP 地址時,它會返回如下響應 -
{'18.236.88.186': {'ip': '18.236.88.186',
'hostname': 'ec2-18-236-88-186.us-west-2.compute.amazonaws.com',
'city': 'Boardman',
'region': 'Oregon',
'country': 'US',
'loc': '45.8399,-119.7006',
'org': 'AS16509 Amazon.com, Inc.',
'postal': '97818',
'timezone': 'America/Los_Angeles',
'country_name': 'United States',
'latitude': '45.8399',
'longitude': '-119.7006'}}
將getBatchDetails
傳遞給Pandas.DataFrame
並轉置 dataframe 將生成 Z6A8064B5DF479455555553
ip | 主機名 | 城市 | 地區 | 國家 | 位置 | 組織 | 郵政 | 時區 | 國家的名字 | 緯度 | 經度 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
64.19.55.14 | 64.19.55.14 | 64.19.55.14.nw.nuvox.net | 小石頭 | 阿肯色州 | 我們 | 34.7871,-92.4222 | AS7029 Windstream Communications LLC | 72212 | 美國/芝加哥 | 美國 | 34.7871 | -92.4222 |
42.41.253.238 | 42.41.253.238 | 楠 | 漢城 | 漢城 | 韓國 | 37.5660,126.9784 | AS9644 SK電訊 | 03141 | 亞洲/首爾 | 韓國 | 37.566 | 126.978 |
44.141.143.69 | 44.141.143.69 | 楠 | 聖地亞哥 | 加利福尼亞 | 我們 | 32.7157,-117.1647 | AS16175 信號 Bredband AS | 92101 | 美國/洛杉磯 | 美國 | 32.7157 | -117.165 |
18.236.88.186 | 18.236.88.186 | ec2-18-236-88-186.us-west-2.compute.amazonaws.com | 博德曼 | 俄勒岡 | 我們 | 45.8399,-119.7006 | AS16509 亞馬遜.com, Inc. | 97818 | 美國/洛杉磯 | 美國 | 45.8399 | -119.701 |
5.186.177.43 | 5.186.177.43 | 楠 | 塔斯楚普 | 首都地區 | 丹麥 | 55.6501,12.3016 | AS44869 FIBIA P/S | 2630 | 歐洲/哥本哈根 | 丹麥 | 55.6501 | 12.3016 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.