Error while trying to append data to columns in Python

丶灬走出姿态 提交于 2021-02-11 15:00:25

问题


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.

I tried using

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

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

Can anybody help me with this


回答1:


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

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

Series.append() only accepts Series objects.

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

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



来源:https://stackoverflow.com/questions/57976658/error-while-trying-to-append-data-to-columns-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!