问题
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