我有一个.csv文件,我想使用,而不是手动输入详细信息来向标记点添加"点击框"



如果我手动输入数据,当我点击谷歌地图标记时,数据会随心所欲地出现在谷歌地图的框中。

这部分都很好。

import gmaps
gmaps.configure(api_key='AI...') 
file_name = [
{'name': 'House A', 'location': (42.162913, 139.487541), 'price': 250},
{'name': 'House B', 'location': (42.171569, 139.514020), 'price': 500},
]
property_locations = [house['location'] for house in file_name]
info_box_template = """
<dl>
<dt>Property Name</dt><dd>{name}</dd>
<dt>Priced from $ </dt><dd>{price}</dd>
</dl>
"""
house_info = [info_box_template.format(**house) for house in file_name]
marker_layer = gmaps.marker_layer(property_locations, info_box_content=house_info)
loc_map = gmaps.figure()
loc_map.add_layer(marker_layer)
loc_map

由于有更多的数据,我想使用上面相同格式的.csv,但我无法使代码正常工作。我怀疑这是因为当我将.csv中的纬度和经度单元格连接到一个新列中以制作一个具有正确经度/经度格式的单元格时,dtype 是"对象",因此收到错误消息。

import gmaps
gmaps.configure(api_key='AI...') 
file_name = pd.read_csv('./house_data.csv')
file_name_1 = pd.DataFrame(file_name, columns= ['name', 'coordinates', 'price'])
property_locations = [house[['coordinates']] for house in file_name_1]
info_box_template = """
<dl>
<dt>Property Name</dt><dd>{name}</dd>
<dt>Priced from $ </dt><dd>{price}</dd>
</dl>
"""
house_info = [info_box_template.format(**house) for house in file_name_1]
marker_layer = gmaps.marker_layer(property_locations, info_box_content=house_info)
loc_map = gmaps.figure()
loc_map.add_layer(marker_layer)
loc_map

TypeError                                 Traceback (most recent call last)
<ipython-input-46-4965661a0fd7> in <module>
3 file_name = pd.read_csv('./house_data.csv')
4 file_name_1 = pd.DataFrame(file_name, columns= ['name','coordinates', 'price'])
----> 5 property_locations = [house[['coordinates']] for house in file_name_1]
6 info_box_template = """
7 <dl>
<ipython-input-46-4965661a0fd7> in <listcomp>(.0)
3 file_name = pd.read_csv('./house_data.csv')
4 file_name_1 = pd.DataFrame(file_name, columns= ['name','coordinates', 'price'])
----> 5 property_locations = [house[['coordinates']] for house in file_name_1]
6 info_box_template = """
7 <dl>
TypeError: string indices must be integers

有没有办法转动位置数据,即。"坐标"成整数?

或者有人可以提出替代方案吗?

您可以在创建数据帧后连接纬度/经度。下面是一个示例:

In [175]: import pandas as pd
In [176]: df = pd.read_csv(r"test.csv")
#null values for location series
In [202]: df
Out[202]:
name  location      lat      long  price
0  house A       NaN  42.1629  139.4875    250
1  house B       NaN  42.1716  139.5140    350

现在连接经度/经度并将值分配给location

In [203]: df.location = tuple(zip(df.lat, df.long))
In [204]: df
Out[204]:
name                 location      lat      long  price
0  house A  (42.162913, 139.487541)  42.1629  139.4875    250
1  house B   (42.171569, 139.51402)  42.1716  139.5140    350

由于gmaps.marker_layer接受元组列表,因此您可以像这样获得location系列:

In [199]: property_locations = df['location'].tolist()
In [200]: property_locations
Out[200]: [(42.162913, 139.487541), (42.171569, 139.51402)]

现在,您可以使用property_locations作为gmaps.marker_layer的参数。

此外,info_box_template.format()需要字典映射。因此,我们必须将数据帧df转换为字典的第一个列表:

In [19]: df_dict = df.to_dict('records')
In [20]: df_dict
Out[20]:
[{'lat': 42.162913,
'location': (42.162913, 139.487541),
'long': 139.487541,
'name': 'house A',
'price': 250},
{'lat': 42.171569,
'location': (42.171569, 139.51402),
'long': 139.51402,
'name': 'house B',
'price': 350}]

现在,使用如下所示df_dict获取每栋房屋的房屋信息:

In [21]: house_info = [info_box_template.format(**house) for house in df_dict]
In [22]: house_info
Out[22]:
['n<dl>n<dt>Property Name</dt><dd>house A</dd>n<dt>Priced from $ </dt><dd>250</dd>n</dl>n',
'n<dl>n<dt>Property Name</dt><dd>house B</dd>n<dt>Priced from $ </dt><dd>350</dd>n</dl>n']

最新更新