在Pandas表中创建列,该列的坐标显示是否找到坐标

2024-06-25 23:59:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个pandas数据框架,如下所示:

df = pd.read_csv("List-of-museums.csv", header='infer', sep=",")
from geopy import geocoders
df2 = df['Name'] #there are multiple museums in the column 'Name'. I would like to get their coordinates 

df2 = df2.tolist()

coordinates = []
for museum in df2: 
    g = geocoder.google(museum)
    coordinates.append(g.latlng)

comb = dict(zip(df2,coordinates))
comb = pd.Series(comb, name='Coordinates')
comb = comb.to_frame()
                             Coordinates
18 Stafford Terrace          [51.500476, -0.1968857]
2 Willow Road                []
575 Wandsworth Road          []
7 Hammersmith Terrace        []
Anaesthesia Heritage Centre  []
Apsley House                 [51.5034719, -0.1516708],[41.5034719,-0.1516708]              

期望输出:

^{pr2}$

因此,我们添加了最后一列,显示是否找到了坐标,如果找到了,有多少。在


Tags: csvtonameinpandasdfpddf2
1条回答
网友
1楼 · 发布于 2024-06-25 23:59:36

我认为你需要:

df = pd.DataFrame({'Name':['x','y','z', 'z1'],
                   'Coordinates':[[[1,2], [2,4]],[4,7],[4,5], []]})

print (df)
        Coordinates Name
0  [[1, 2], [2, 4]]    x
1            [4, 7]    y
2            [4, 5]    z
3                []   z1

def recursive_len(item):
    if type(item) == list:
        return sum(recursive_len(subitem) for subitem in item)
    else:
        return 1

df['count'] = df['Coordinates'].apply(recursive_len) // 2
print (df)
        Coordinates Name  count
0  [[1, 2], [2, 4]]    x      2
1            [4, 7]    y      1
2            [4, 5]    z      1
3                []   z1      0

另一个解决方案是:

^{pr2}$

相关问题 更多 >