带有 df 的 Geopandas

Geopandas with df

我有包含 location, side_a, and side_b 的数据框,它们是参与 war 的国家。 我还有每个 war.

的死亡人数、开始年份等详细信息

假设我想使用 geopandas 显示每个州的死亡人数,我该怎么做? 我尝试使用此代码,但它只是给了我一张世界图(列 = 死亡人数):

fig, ax = plt.subplots(1, 1)
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world = world[(world.pop_est>0) & (world.name!="Antarctica")]
world[column] = df[column]
world.plot(column=column, ax=ax, legend=True)

例如,我希望这个 df 成为一个图表,其中所有状态都是彩色的:

war_index 位置 死亡人数
1 印度、中国 20
2 印度 10

那中国冷一个颜色代表10,印度其他颜色代表30 其余的不会被着色

方法是在 world 中添加一个包含死亡人数的列。因此,首先您需要计算每个国家 df 的总死亡人数。您可以通过拆分 location 中的字符串来做到这一点。然后展开该列并在该列上 运行 一个 groupby。随后,您可以使用 pd.merge 合并 name/location 上的数据帧,然后通过 death number:

绘制 world
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([ { "war_index": 1, "location": "India, China", "death number": 20 }, { "war_index": 2, "location": "India", "death number": 10 } ])
df['location'] = df['location'].str.split(', ')
df = df.explode('location').groupby('location').agg('sum').reset_index()

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.pop_est>0) & (world.name!="Antarctica")]
world = pd.merge(world,df[['location','death number']], left_on='name', right_on='location', how='left')

fig, ax = plt.subplots(1, 1)
world.plot(column='death number', ax=ax, legend=True)

结果:

如果你还想显示世界其他地方,你可以将death number列中的nan替换为0。

world['death number'] = world['death number'].fillna(0)