尝试将数据附加到 Python 中的列时出错

Error while trying to append data to columns in Python

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

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")


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

我试过使用

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

它给我所有的行 'Westland Avenue' 结果应该如下

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

谁能帮我解决这个问题

我在一些示例数据上尝试了您的代码,但在这样做时遇到了这个错误:

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 然后附加它。