pandas教学--如何制作全国各地xxx系列

本文介绍了如何使用Python库pandas和geopandas生成随机数据,以及如何将这些数据应用到中国各省的压岁钱分布图上,展示了数据修改和地图绘图的过程。
摘要由CSDN通过智能技术生成

前言

微博刷到一张营销号瞎整的全国各地压岁钱分布图
定睛一看广东省竟然高达五十元,这当然是假的啦
我们都是五块十块,你直接给翻了数量级
吓得笔者赶紧写一个pandas简易教程

随机的数据生成

In [7]:

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)
   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]:

## 读取随机生成的压岁钱数据
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

Out[13]:

省级码省类型ENG_NAMEVAR_NAMEFIRST_GIDFIRST_TYPEyeargeometryRandom
0北京市110000直辖市BeijingBěi Jīng110000Municipality2022POLYGON ((117.38335 40.22647, 117.38557 40.224...93.502211
1天津市120000直辖市TianjinTiān Jīn120000Municipality2022POLYGON ((117.56937 40.19153, 117.56744 40.189...79.003211
2河北省130000HebeiHé Běi130000Province2022MULTIPOLYGON (((118.26945 38.98097, 118.26871 ...97.344063
3山西省140000ShanxiShān Xī140000Province2022POLYGON ((114.13714 40.73445, 114.13860 40.732...69.052652
4内蒙古自治区150000自治区NeimengguNèi Měng Gǔ150000Autonomous Region2022POLYGON ((121.49813 53.32607, 121.50116 53.321...51.146289
5辽宁省210000LiaoningLiáo Níng210000Province2022MULTIPOLYGON (((121.03521 38.87021, 121.03528 ...24.687464
6吉林省220000JilinJí Lín220000Province2022POLYGON ((123.90309 46.29744, 123.90283 46.294...55.642060
7黑龙江省230000HeilongjiangHēi Lóng Jiāng230000Province2022POLYGON ((123.40249 53.53506, 123.40471 53.535...68.246176
8上海市310000直辖市ShanghaiShàng Hǎi310000Municipality2022MULTIPOLYGON (((121.87476 31.63516, 121.87542 ...16.877693
9浙江省330000ZhejiangZhè Jiāng330000Province2022MULTIPOLYGON (((120.47933 27.15321, 120.48163 ...93.926350
10安徽省340000Anhuiān Huī340000Province2022POLYGON ((116.42485 34.65234, 116.43225 34.642...0.832999
11福建省350000FujianFú Jiàn350000Province2022MULTIPOLYGON (((117.29228 23.59563, 117.29206 ...95.358016
12江西省360000JiangxiJiāng Xī360000Province2022POLYGON ((116.68416 30.07160, 116.68576 30.070...1.049879
13山东省370000ShandongShān Dōng370000Province2022MULTIPOLYGON (((119.92414 35.62384, 119.92294 ...82.199015
14河南省410000HenanHé Nán410000Province2022MULTIPOLYGON (((111.02770 33.17911, 111.02767 ...14.491251
15湖北省420000HubeiHú Běi420000Province2022MULTIPOLYGON (((113.12740 29.43223, 113.11645 ...0.626169
16湖南省430000HunanHú Nán430000Province2022MULTIPOLYGON (((109.47771 26.84005, 109.47793 ...27.848135
17广东省440000GuangdongGuǎng Dōng440000Province2022MULTIPOLYGON (((110.59023 20.37852, 110.59232 ...54.834726
18广西壮族自治区450000自治区GuangxiGuǎng Xī450000Autonomous Region2022MULTIPOLYGON (((109.20674 20.91898, 109.20686 ...18.049076
19海南省460000HainanHǎi Nán460000Province2022MULTIPOLYGON (((112.04381 3.83812, 112.01370 3...14.530838
20重庆市500000直辖市ChongqingChóng Qìng500000Municipality2022POLYGON ((109.57960 31.72849, 109.58644 31.725...24.778711
21四川省510000SichuanSì Chuān510000Province2022POLYGON ((102.95840 34.27996, 102.95933 34.270...6.216893
22贵州省520000GuizhouGuì Zhōu520000Province2022MULTIPOLYGON (((105.09467 24.92520, 105.09458 ...15.550355
23云南省530000YunnanYún Nán530000Province2022POLYGON ((99.11276 29.21149, 99.11737 29.20723...67.450696
24西藏自治区540000自治区XizangXī Zàng540000Autonomous Region2022POLYGON ((88.38821 36.47854, 88.38945 36.47845...73.610347
25陕西省610000ShaanxiShǎn Xī610000Province2022POLYGON ((108.13454 36.57919, 108.13418 36.580...14.560607
26甘肃省620000GansuGān Sù620000Province2022POLYGON ((97.19051 42.76287, 97.23601 42.67222...74.807518
27青海省630000QinghaiQīng Hǎi630000Province2022POLYGON ((100.91694 38.17344, 100.91780 38.173...80.159599
28宁夏回族自治区640000自治区NingxiaNíng Xià Huí Zú640000Autonomous Region2022MULTIPOLYGON (((106.06218 35.43728, 106.06239 ...10.338105
29新疆维吾尔自治区650000自治区XinjiangXīn Jiāng650000Autonomous Region2022POLYGON ((87.79720 49.18060, 87.81916 49.17268...55.175422
30台湾省710000TaiwanTái Wān710000Province2022MULTIPOLYGON (((123.69793 25.92930, 123.69726 ...53.143895
31香港特别行政区810000特别行政区HongKongHong Kong810000Special District2022MULTIPOLYGON (((114.22665 22.54375, 114.22661 ...96.667167
32澳门特别行政区820000特别行政区Aomenào Mén820000Special District2022MULTIPOLYGON (((113.55346 22.21547, 113.55374 ...39.390123
33江苏省320000JiangsuJiāng Sū320000Province2022MULTIPOLYGON (((121.56617 32.22928, 121.56693 ...0.658625

修改某行某列随机值

In [29]:

shp.loc[shp['省'] == '广东省', 'Random'] = 10

geopandas版绘图

In [30]:

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()
<Figure size 1440x864 with 0 Axes>

cartopy版绘图

In [31]:

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

虽说是临时之作,但数据生成,修改数值,数据绘图都包揽了
geopandas也是pandas

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴躁的秋秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值