尝试将数据附加到 Python 中的列时出错 [英] Error while trying to append data to columns in Python

查看:20
本文介绍了尝试将数据附加到 Python 中的列时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反转地理编码数据,为此我有以下查询

I am trying to reverse geocode data and for that I have below query

import overpy
import pandas as pd
import numpy as np

df = pd.read_csv("/home/runner/sample.csv")
df.sort_values(by=['cvdt35_timestamp_s'],inplace=True)

api= overpy.Overpass()
box = 0.0005
queries = []
results = []
df['Name']=''
df['Highway'] =''

with open("sample.csv") as f:
  for row in df.index:
    query = 'way('+str(df.gps_lat_dd.iloc[row]-box)+','+str(df.gps_lon_dd.iloc[row]-box)+','+str(df.gps_lat_dd.iloc[row]+box)+','+str(df.gps_lon_dd.iloc[row]+box)+') ["highway"]; (._;>;); out body;'
    queries.append(query)
  for query in queries :
    result = api.query(query)
    results.append(result)
  for result in results:
    for way in result.ways:
        name = way.tags.get("name", "n/a")
        df['Name'].append(name)
    for way in result.ways:  
        df['Highway']= way.tags.get("highway", "n/a")


我试图将每个结果附加到数据框中的新列,但上面的代码抛出错误.

I am trying to append each result to new column in the data frame but the above code is throwing error.

我尝试使用

for way in result.ways:
        df['Name'] = way.tags.get("name", "n/a")

它给我的所有行都是Westland Avenue",结果应该如下

it's giving me all the rows as 'Westland Avenue' where as result should be as follows

Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Brookville Road
Westland Avenue

谁能帮我解决这个问题

推荐答案

我在一些示例数据上尝试了您的代码,但在执行此操作时遇到此错误:

I tried your code on some sample data and got this error while doing so:

TypeError: 无法连接类型为"的对象;只要pd.Series、pd.DataFrame 和 pd.Panel(已弃用)对象有效

TypeError: cannot concatenate object of type "<class 'int'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

Series.append() 只接受 Series 对象.

试试df['Name'].append(pd.Series(name)).

或者更好的是,创建这些名称的列表,将列表转换为 Series,然后附加它.

Or better yet, create a list of these names, convert the list to a Series and then append it.

这篇关于尝试将数据附加到 Python 中的列时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆