前言
微博刷到一张营销号瞎整的全国各地压岁钱分布图 定睛一看广东省竟然高达五十元,这当然是假的啦 我们都是五块十块,你直接给翻了数量级 吓得笔者赶紧拿起键盘写一个pandas简易教程
随机的数据生成
In [7]:
代码语言:javascript复制
代码语言:javascript复制import pandas as pd
import numpy as np
# 创建一个示例的DataFrame
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
# 生成随机数列
random_nums = np.random.rand(len(df))
# 将随机数列添加为新的列
df['Random'] = random_nums
# 打印结果
print(df)
代码语言:javascript复制
代码语言:javascript复制 A B Random
0 1 6 0.679361
1 2 7 0.475995
2 3 8 0.729500
3 4 9 0.972659
4 5 10 0.523726
实际应用
In [13]:
代码语言:javascript复制
代码语言:javascript复制## 读取随机生成的压岁钱数据
import pandas as pd
import geopandas as gpd
# 读取shp文件
shp = gpd.read_file('/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp')
random_nums = 100*np.random.rand(len(shp))
# 将随机数列添加为新的列
shp['Random'] = random_nums
shp
代码语言:javascript复制
Out[13]:
省 | 省级码 | 省类型 | ENG_NAME | VAR_NAME | FIRST_GID | FIRST_TYPE | year | geometry | Random | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 北京市 | 110000 | 直辖市 | Beijing | Běi Jīng | 110000 | Municipality | 2022 | POLYGON ((117.38335 40.22647, 117.38557 40.224... | 93.502211 |
1 | 天津市 | 120000 | 直辖市 | Tianjin | Tiān Jīn | 120000 | Municipality | 2022 | POLYGON ((117.56937 40.19153, 117.56744 40.189... | 79.003211 |
2 | 河北省 | 130000 | 省 | Hebei | Hé Běi | 130000 | Province | 2022 | MULTIPOLYGON (((118.26945 38.98097, 118.26871 ... | 97.344063 |
3 | 山西省 | 140000 | 省 | Shanxi | Shān Xī | 140000 | Province | 2022 | POLYGON ((114.13714 40.73445, 114.13860 40.732... | 69.052652 |
4 | 内蒙古自治区 | 150000 | 自治区 | Neimenggu | Nèi Měng Gǔ | 150000 | Autonomous Region | 2022 | POLYGON ((121.49813 53.32607, 121.50116 53.321... | 51.146289 |
5 | 辽宁省 | 210000 | 省 | Liaoning | Liáo Níng | 210000 | Province | 2022 | MULTIPOLYGON (((121.03521 38.87021, 121.03528 ... | 24.687464 |
6 | 吉林省 | 220000 | 省 | Jilin | Jí Lín | 220000 | Province | 2022 | POLYGON ((123.90309 46.29744, 123.90283 46.294... | 55.642060 |
7 | 黑龙江省 | 230000 | 省 | Heilongjiang | Hēi Lóng Jiāng | 230000 | Province | 2022 | POLYGON ((123.40249 53.53506, 123.40471 53.535... | 68.246176 |
8 | 上海市 | 310000 | 直辖市 | Shanghai | Shàng Hǎi | 310000 | Municipality | 2022 | MULTIPOLYGON (((121.87476 31.63516, 121.87542 ... | 16.877693 |
9 | 浙江省 | 330000 | 省 | Zhejiang | Zhè Jiāng | 330000 | Province | 2022 | MULTIPOLYGON (((120.47933 27.15321, 120.48163 ... | 93.926350 |
10 | 安徽省 | 340000 | 省 | Anhui | ān Huī | 340000 | Province | 2022 | POLYGON ((116.42485 34.65234, 116.43225 34.642... | 0.832999 |
11 | 福建省 | 350000 | 省 | Fujian | Fú Jiàn | 350000 | Province | 2022 | MULTIPOLYGON (((117.29228 23.59563, 117.29206 ... | 95.358016 |
12 | 江西省 | 360000 | 省 | Jiangxi | Jiāng Xī | 360000 | Province | 2022 | POLYGON ((116.68416 30.07160, 116.68576 30.070... | 1.049879 |
13 | 山东省 | 370000 | 省 | Shandong | Shān Dōng | 370000 | Province | 2022 | MULTIPOLYGON (((119.92414 35.62384, 119.92294 ... | 82.199015 |
14 | 河南省 | 410000 | 省 | Henan | Hé Nán | 410000 | Province | 2022 | MULTIPOLYGON (((111.02770 33.17911, 111.02767 ... | 14.491251 |
15 | 湖北省 | 420000 | 省 | Hubei | Hú Běi | 420000 | Province | 2022 | MULTIPOLYGON (((113.12740 29.43223, 113.11645 ... | 0.626169 |
16 | 湖南省 | 430000 | 省 | Hunan | Hú Nán | 430000 | Province | 2022 | MULTIPOLYGON (((109.47771 26.84005, 109.47793 ... | 27.848135 |
17 | 广东省 | 440000 | 省 | Guangdong | Guǎng Dōng | 440000 | Province | 2022 | MULTIPOLYGON (((110.59023 20.37852, 110.59232 ... | 54.834726 |
18 | 广西壮族自治区 | 450000 | 自治区 | Guangxi | Guǎng Xī | 450000 | Autonomous Region | 2022 | MULTIPOLYGON (((109.20674 20.91898, 109.20686 ... | 18.049076 |
19 | 海南省 | 460000 | 省 | Hainan | Hǎi Nán | 460000 | Province | 2022 | MULTIPOLYGON (((112.04381 3.83812, 112.01370 3... | 14.530838 |
20 | 重庆市 | 500000 | 直辖市 | Chongqing | Chóng Qìng | 500000 | Municipality | 2022 | POLYGON ((109.57960 31.72849, 109.58644 31.725... | 24.778711 |
21 | 四川省 | 510000 | 省 | Sichuan | Sì Chuān | 510000 | Province | 2022 | POLYGON ((102.95840 34.27996, 102.95933 34.270... | 6.216893 |
22 | 贵州省 | 520000 | 省 | Guizhou | Guì Zhōu | 520000 | Province | 2022 | MULTIPOLYGON (((105.09467 24.92520, 105.09458 ... | 15.550355 |
23 | 云南省 | 530000 | 省 | Yunnan | Yún Nán | 530000 | Province | 2022 | POLYGON ((99.11276 29.21149, 99.11737 29.20723... | 67.450696 |
24 | 西藏自治区 | 540000 | 自治区 | Xizang | Xī Zàng | 540000 | Autonomous Region | 2022 | POLYGON ((88.38821 36.47854, 88.38945 36.47845... | 73.610347 |
25 | 陕西省 | 610000 | 省 | Shaanxi | Shǎn Xī | 610000 | Province | 2022 | POLYGON ((108.13454 36.57919, 108.13418 36.580... | 14.560607 |
26 | 甘肃省 | 620000 | 省 | Gansu | Gān Sù | 620000 | Province | 2022 | POLYGON ((97.19051 42.76287, 97.23601 42.67222... | 74.807518 |
27 | 青海省 | 630000 | 省 | Qinghai | Qīng Hǎi | 630000 | Province | 2022 | POLYGON ((100.91694 38.17344, 100.91780 38.173... | 80.159599 |
28 | 宁夏回族自治区 | 640000 | 自治区 | Ningxia | Níng Xià Huí Zú | 640000 | Autonomous Region | 2022 | MULTIPOLYGON (((106.06218 35.43728, 106.06239 ... | 10.338105 |
29 | 新疆维吾尔自治区 | 650000 | 自治区 | Xinjiang | Xīn Jiāng | 650000 | Autonomous Region | 2022 | POLYGON ((87.79720 49.18060, 87.81916 49.17268... | 55.175422 |
30 | 台湾省 | 710000 | 省 | Taiwan | Tái Wān | 710000 | Province | 2022 | MULTIPOLYGON (((123.69793 25.92930, 123.69726 ... | 53.143895 |
31 | 香港特别行政区 | 810000 | 特别行政区 | HongKong | Hong Kong | 810000 | Special District | 2022 | MULTIPOLYGON (((114.22665 22.54375, 114.22661 ... | 96.667167 |
32 | 澳门特别行政区 | 820000 | 特别行政区 | Aomen | ào Mén | 820000 | Special District | 2022 | MULTIPOLYGON (((113.55346 22.21547, 113.55374 ... | 39.390123 |
33 | 江苏省 | 320000 | 省 | Jiangsu | Jiāng Sū | 320000 | Province | 2022 | MULTIPOLYGON (((121.56617 32.22928, 121.56693 ... | 0.658625 |
修改某行某列随机值
In [29]:
代码语言:javascript复制
代码语言:javascript复制shp.loc[shp['省'] == '广东省', 'Random'] = 10
代码语言:javascript复制
geopandas版绘图
In [30]:
代码语言:javascript复制
代码语言:javascript复制import matplotlib.pyplot as plt
# 画布设置
plt.figure(figsize=(20, 12))
# 绘制各省份的随机值分布图
shp.plot(column='Random', cmap='OrRd', linewidth=0.8, edgecolor='0.8', legend=True)
# 在每个地方标上数值
for idx, row in shp.iterrows():
plt.text(row.geometry.centroid.x, row.geometry.centroid.y, round(row['Random'], 2), ha='center', fontsize=8)
# 设置标题
plt.title('中国各省份随机值分布图', fontweight='bold', fontsize=15)
# 显示图片
plt.show()
代码语言:javascript复制
代码语言:javascript复制<Figure size 1440x864 with 0 Axes>
cartopy版绘图
In [31]:
代码语言:javascript复制
代码语言:javascript复制import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
# 创建地图投影
fig = plt.figure(figsize=(20, 12))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
# 绘制各省份的边界和随机值填充色
for idx, row in shp.iterrows():
ax.add_geometries([row['geometry']], ccrs.PlateCarree(), facecolor=plt.cm.OrRd(row['Random']/100), edgecolor='black', linewidth=0.8)
# 在每个地方标上数值
for idx, row in shp.iterrows():
ax.text(row.geometry.centroid.x, row.geometry.centroid.y, round(row['Random'], 2), fontsize=8, ha='center', transform=ccrs.PlateCarree())
# 添加海岸线、国家边界等地图要素
ax.add_feature(cfeature.COASTLINE)
# 设置地图范围
ax.set_extent([70, 140, 15, 55])
# 设置标题
plt.title('中国各省份随机值分布图', fontweight='bold', fontsize=15)
# 显示地图
plt.show()
代码语言:javascript复制
虽说是临时之作,但数据生成,修改数值,数据绘图都包揽了 geopandas也是pandas
点击链接可在线运行程序