数据储存在:https://raw.githubusercontent.com/HEPINGSUN/Maine-Data/main/Maine%20data.csv%EF%BC%8C 这个csv文件中每一列分别为:“OBJECTID”,“Geographic Identifier - FIPS Code”,“Name“,“State“,“County“,“Total households“,“Percent of Households that Have No Computer, Smartphone, or Tablet“,“Percent of Households with No Internet Access”,“Ookla Median Download Speed (Mbps)”,“Ookla Median Upload Speed (Mbps)”以及“Population 2019 (FCC Estimate)”。我的需求是提取“Geographic Identifier - FIPS Code”中以23开头也就是Maine州的数据。计算每一个county所占State的人口百分比记录为“Percent of population”。然后按照0.3“Percent of population+0.3 Percent of Households that Have No Computer, Smartphone, or Tablet+0.3.* Percent of Households with No Internet Access-0.05* Ookla Median Download Speed (Mbps)-0.05* Ookla Median Upload Speed (Mbps)这个公式得到一个值名为INFA,这个值应该对应的是每一个county,使用python实现,并在网站中呈现出一个交互式的地图。
这个问题可以通过gat解决,试试怎么解决
不知道你这个问题是否已经解决, 如果还没有解决的话:答案:
pandas
模块中的read_csv()
来完成这个步骤:import pandas as pd
url = 'https://raw.githubusercontent.com/HEPINGSUN/Maine-Data/main/Maine%20data.csv'
df = pd.read_csv(url)
str
方法来完成这个步骤:fips = df[df['Geographic Identifier - FIPS Code'].str.startswith('23')]
sum()
函数来实现:total_population = fips.groupby('County')['Population 2019 (FCC Estimate)'].sum().reset_index(name='Total Population')
total_population
数据框中。total_population['INFA'] = (0.3 * fips.groupby('County')['Percent of Households that Have No Computer, Smartphone, or Tablet'].mean()) + \
(0.3 * fips.groupby('County')['Percent of Households with No Internet Access'].mean()) - \
(0.05 * fips.groupby('County')['Ookla Median Download Speed (Mbps)'].mean()) - \
(0.05 * fips.groupby('County')['Ookla Median Upload Speed (Mbps)'].mean())
folium
库:!pip install folium
import folium
m = folium.Map(location=[45.2538, -69.4455], zoom_start=7)
这将创建一个地图,并将地图的初始位置设置为缩放级别为7的Maine州中心。
for _, row in total_population.iterrows():
name = row['County'] + ', Maine'
infavalue = round(row['INFA'], 2)
populationvalue = int(row['Total Population'])
folium.Marker(
location=[fips.loc[fips['County'] == row['County']]['Latitude'].iloc[0],
fips.loc[fips['County'] == row['County']]['Longitude'].iloc[0]],
popup=f'<strong>{name}</strong><br>INFA Value: {infavalue}<br>Total Population: {populationvalue}',
tooltip=name,
icon=folium.Icon(color='blue')
).add_to(m)
m
完整代码如下: