Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

python - 我有一个要使用的.csv文件,而不是手动输入详细信息以向标记点添加“点击框”(I have a .csv file I'd like to use instead of manually entering the details to add a 'click box' to a marker point)

If I manually enter data, when I click the Google Map marker, the data appears in the box on Google Maps as I'd like.

(如果我手动输入数据,则当我单击Google Map标记时,数据会根据需要显示在Google Maps的框中。)

This section is all fine.

(这部分很好。)

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

As there is a lot more data, I'd like to use a .csv with the same format above but I can't make the code work correctly.

(由于有更多的数据,我想使用具有上述相同格式的.csv,但无法使代码正常工作。)

I suspect it is because when I concatenated the latitude and longitude cells from the .csv into a new column to make one cell with correct lat/long format, the dtype is 'object' so receiving the error message.

(我怀疑这是因为,当我将.csv中的纬度和经度单元连接到一个新列中,以使一个单元格具有正确的经/纬度格式时,dtype是'object',因此会收到错误消息。)

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

Is there a way to turn location data ie.

(有没有一种方法来转向位置数据即。)

'coordinates' into integers?

(将“坐标”转换为整数?)

Or could someone suggest an alternative?

(还是有人建议替代?)

  ask by jat7709 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can concatenate lat/long after creating the dataframe.

(您可以在创建数据框后并置经/纬度。)

Here's an example:

(这是一个例子:)

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

Now concatenate lat/long and assign values to location :

(现在连接经/纬度并将值分配给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

As gmaps.marker_layer accepts a list of tuples, you can get that for the location series like this:

(当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)]

Now, you can use property_locations as an argument for gmaps.marker_layer .

(现在,您可以将property_locations用作gmaps.marker_layer的参数。)

Also, info_box_template.format() requires a dictionary mapping.

(另外, info_box_template.format()需要字典映射。)

Thus, we must transform the dataframe df to a list of dict's first:

(因此,我们必须将数据帧df转换为dict的第一个列表:)

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}]

Now, use df_dict like below to get house info for each house:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...