1

我已经使用 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
4

1 回答 1

0

请注意错误:

AttributeError: 'IP2LocationRecord' object ...

您要做的是将无法直接转换为类型的对象(您必须直接解压缩所有值,或从 的字段中创建字典IP2LocationRecord)。你在这里看到的:pandas.DataFrameIP2LocationRecord

{'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 参考(而不是字典)

于 2021-01-30T16:05:42.390 回答