pandas 在Python中绘制地理空间可视化

jm81lzqq  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(178)

我有一个Pandas的数据框,看起来像这样:
| 售货员|连|销售额|面积|
| - ------|- ------|- ------|- ------|
| 美国广播公司|AA|小行星54000|万隆|
| 定义|BB型|小行星23000|茂物|
| 吉|公元前|小行星54000|万隆|
| 杰克尔|数据元素|小行星23000|茂物|
| 美国广播公司|IJ|小行星54000|万隆|
| 吉|吉隆坡|小行星23000|茂物|
我想根据每个salesperson的面积来可视化上面company数据的数量。问题是,我没有面积坐标的数据。
这是我想要在Map中绘制的数据输出:

salesperson  area
abc          bandung    2
def          bogor      1
ghi          bandung    1
ghi          bogor      1
jkl          bogor      1

是否可以在没有坐标数据的情况下绘制地理空间可视化效果?或者是否应该查找区域坐标数据来绘制地理空间可视化效果?

dxxyhpgq

dxxyhpgq1#

您需要获取这些位置的坐标,因此首先执行以下操作,将该数据附加到 Dataframe :

import pandas as pd
import geopy
from geopy.geocoders import Nominatim

data = {
    'salesperson': ['abc', 'def', 'ghi', 'jkl', 'abc', 'ghi'],
    'company': ['AA', 'BB', 'BC', 'DE', 'IJ', 'KL'],
    'sales_amount': [54000, 23000, 54000, 23000, 54000, 23000],
    'area': ['bandung', 'bogor', 'bandung', 'bogor', 'bandung', 'bogor']
}
df = pd.DataFrame(data)

geolocator = Nominatim(user_agent='my_application')

def get_lat_lon(location):
    try:
        # Use the geolocator to obtain the location information
        loc = geolocator.geocode(location)
        # Extract the latitude and longitude
        lat, lon = loc.latitude, loc.longitude
        return lat, lon
    except:
        return None, None

df['latitude'], df['longitude'] = zip(*df['area'].apply(get_lat_lon))

## If you get a set copy warning, replace the above line by
#df['latitude'] = None
#df['longitude'] = None
#for index, row in df.iterrows():
#    lat, lon = get_lat_lon(row['area'])
#    df.loc[index, 'latitude'] = lat
#    df.loc[index, 'longitude'] = lon

# Print the dataframe with latitude and longitude columns
print(df)

该函数返回:

salesperson company  sales_amount     area  latitude   longitude
0         abc      AA         54000  bandung -6.934469  107.604954
1         def      BB         23000    bogor -6.596299  106.797242
2         ghi      BC         54000  bandung -6.934469  107.604954
3         jkl      DE         23000    bogor -6.596299  106.797242
4         abc      IJ         54000  bandung -6.934469  107.604954
5         ghi      KL         23000    bogor -6.596299  106.797242

然后Map:

import folium
from folium.plugins import MarkerCluster

center = [-6.1754, 106.8272]  # Jakarta, Indonesia

map_sales = folium.Map(location=center, zoom_start=8)

marker_cluster = MarkerCluster().add_to(map_sales)

for index, row in df.iterrows():
    lat, lon = row['latitude'], row['longitude']
    # Create a popup with the salesperson and the number of companies in the area
    popup_text = f"Salesperson: {row['salesperson']}<br>Number of companies: {row['area']}<br>Sales amount: {row['sales_amount']}"
    folium.Marker(location=[lat, lon], popup=popup_text).add_to(marker_cluster)
    folium.CircleMarker(location=[lat, lon], radius=row['sales_amount']/10000,
                        fill_color='red', color=None, fill_opacity=0.2).add_to(map_sales)

map_sales

相关问题