Python 为什么赢了';我的程序不接受熊猫数据系列作为输入吗?

Python 为什么赢了';我的程序不接受熊猫数据系列作为输入吗?,python,api,Python,Api,为一个学校项目做这个。我基本上是从维基百科历史中删除IP地址。然后,我通过ipstack.com API运行IP地址,得到lat和long。然后,我试图将lat和long推送到opencageapi,但这就是我遇到的问题。如果我硬编码一个lat,然后长时间输入它,它会返回一个城市 result = geocoder.opencage([latitude, longitude], key=key, method='reverse') print(result.city) 但当我尝试循环遍历一个l

为一个学校项目做这个。我基本上是从维基百科历史中删除IP地址。然后,我通过ipstack.com API运行IP地址,得到lat和long。然后,我试图将lat和long推送到opencageapi,但这就是我遇到的问题。如果我硬编码一个lat,然后长时间输入它,它会返回一个城市

result = geocoder.opencage([latitude, longitude], key=key, method='reverse')
print(result.city)
但当我尝试循环遍历一个lat和long列表时,我得到了一个错误

TypeError: cannot convert the series to <class 'float'>

我认为它与你正在通过的类型相混淆:

不确定数据的确切结构,但请尝试以下方法:

latitude = ip_df['latitude'].astype(float)
longitude = ip_df['longitude'].astype(float)

你的实施很粗糙。我会这样做

def make_city(row):
    result = geocoder.opencage(float(row['latitude']), #lat of target
                               float(row['longitude']), #long of target
                               key=key, #API key that I will keep to myself
                               method='reverse')
    print(result.city)

ip_df.apply(make_city, axis = 1)

您可能不应该像这样公开API访问密钥:)Doh!谢谢我没有注意到这一点,也没有太担心,因为这些都是免费服务。我可以请求一个新的API键。纬度和经度是一个数据系列,每个值都已经是一个浮点数,这让它很奇怪。类型(经度),类型(经度[0])获取错误。虽然它可能在引号中有纬度和经度,但得到了相同的错误_validate_read_indexer key=key,axis=self.obj._get_axis_name(axis)))keyrerror:(“[42.7376,51.3751.3751,32.8473,30.3631,37.751,37.751,39.7392,43.3884,42.9403,37.751,42.769,42.7851,42.7851,43.2506,37.751,37.751]中没有一个在[索引]中,”,实际上“发生在索引城市”,这段代码给出了上面的错误:ip_df.apply(make_city)。之前的代码只是为结果变量生成一个空的类“list”,以及lat和long周围的括号。有一次我发现它工作了,尽管它确实返回了一些坐标的“none”。
def make_city(row):
    result = geocoder.opencage(float(row['latitude']), #lat of target
                               float(row['longitude']), #long of target
                               key=key, #API key that I will keep to myself
                               method='reverse')
    print(result.city)

ip_df.apply(make_city, axis = 1)