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

查看:50
本文介绍了尝试将数据追加到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:无法连接类型为< class'int'>"的对象;只要pd.Series,pd.DataFrame和pd.Panel(不建议使用)objs有效

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天全站免登陆