Example - Merge For Map
Table of Contents
1) การรวมตารางเพื่อพลอตค่าต่าง ๆ ในแผนที่
import pandas as pd
นำเข้าตารางที่มีค่าที่ต้องการนำมาพลอต
df = pd.read_excel("./confirmed-cases-since-271064.xlsx")
df.head()
No. | announce_date | Notified date | sex | age | Unit | nationality | province_of_isolation | risk | province_of_onset | district_of_onset | Unnamed: 11 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1859158 | 2021-10-26 | 2021-10-25 | ชาย | 20 | ปี | Thailand | ลำปาง | อื่นๆ | ลำปาง | เมืองลำปาง | NaN |
1 | 1859159 | 2021-10-26 | 2021-10-25 | หญิง | 42 | ปี | Thailand | ลำปาง | อื่นๆ | ลำปาง | เมืองลำปาง | NaN |
2 | 1859160 | 2021-10-26 | 2021-10-25 | หญิง | 33 | ปี | Thailand | ลำปาง | ไปสถานที่ชุมชน เช่น ตลาดนัด สถานที่ท่องเที่ยว | ลำปาง | ห้างฉัตร | NaN |
3 | 1859161 | 2021-10-26 | 2021-10-25 | หญิง | 52 | ปี | Thailand | ลำปาง | ไปสถานที่ชุมชน เช่น ตลาดนัด สถานที่ท่องเที่ยว | ลำปาง | ห้างฉัตร | NaN |
4 | 1859162 | 2021-10-26 | 2021-10-25 | หญิง | 84 | ปี | Thailand | ลำปาง | สัมผัสใกล้ชิดกับผู้ป่วยยืนยันรายก่อนหน้านี้ | ลำปาง | เมืองลำปาง | NaN |
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1010459 entries, 0 to 1010458
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 No. 1010459 non-null int64
1 announce_date 1010459 non-null datetime64[ns]
2 Notified date 1010459 non-null datetime64[ns]
3 sex 951665 non-null object
4 age 923950 non-null object
5 Unit 903642 non-null object
6 nationality 922053 non-null object
7 province_of_isolation 1006775 non-null object
8 risk 1010380 non-null object
9 province_of_onset 899173 non-null object
10 district_of_onset 865730 non-null object
11 Unnamed: 11 0 non-null float64
dtypes: datetime64[ns](2), float64(1), int64(1), object(8)
memory usage: 92.5+ MB
จัดการตารางให้มีค่าที่ต้องการพลอต
ในที่นี้คือจำนวนเคสในแต่ละ province_of_isolation
isodf = df["province_of_isolation"].value_counts().reset_index()
isodf
index | province_of_isolation | |
---|---|---|
0 | กรุงเทพมหานคร | 144807 |
1 | สมุทรปราการ | 55918 |
2 | ชลบุรี | 53832 |
3 | นครศรีธรรมราช | 39358 |
4 | ภูเก็ต | 36047 |
... | ... | ... |
75 | สิงห์บุรี | 1810 |
76 | อำนาจเจริญ | 1789 |
77 | ขอนแก่น | 66 |
78 | กทม | 5 |
79 | ภููเก็ต | 1 |
80 rows × 2 columns
2) อ่านตารางแผนที่ประเทศไทย
โดยโหลดไฟล์เข้ามาก่อน
!wget https://bit.ly/thaigeojson
--2022-08-01 03:16:13-- https://bit.ly/thaigeojson
Resolving bit.ly (bit.ly)... 67.199.248.11, 67.199.248.10
Connecting to bit.ly (bit.ly)|67.199.248.11|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://raw.githubusercontent.com/vistec-AI/colab/master/thailand.json [following]
--2022-08-01 03:16:13-- https://raw.githubusercontent.com/vistec-AI/colab/master/thailand.json
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 195042 (190K) [text/plain]
Saving to: ‘thaigeojson’
thaigeojson 100%[===================>] 190.47K --.-KB/s in 0.07s
2022-08-01 03:16:14 (2.51 MB/s) - ‘thaigeojson’ saved [195042/195042]
ใช้ geopandas
ในการอ่านข้อมูลแผนที่
import geopandas as gpd
import plotly.express as px
mapdf = gpd.read_file('thaigeojson')
mapdf.head()
code | name | tname | area | geometry | |
---|---|---|---|---|---|
0 | 10 | Bangkok | กรุงเทพมหานคร | 1568.95 | POLYGON ((100.85537 13.69198, 100.71321 13.711... |
1 | 11 | Samut Prakan | สมุทรปราการ | 960.09 | POLYGON ((100.85537 13.69198, 100.94033 13.657... |
2 | 12 | Nonthaburi | นนทบุรี | 633.25 | POLYGON ((100.35029 14.11371, 100.33659 14.060... |
3 | 13 | Pathum Thani | ปทุมธานี | 1520.06 | POLYGON ((100.91242 14.21391, 100.90529 13.955... |
4 | 14 | Phra Nakhon Si Ayutthaya | พระนครศรีอยุธยา | 2534.07 | POLYGON ((100.59405 14.65593, 100.57436 14.589... |
mapdf.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 77 entries, 0 to 76
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 code 77 non-null object
1 name 77 non-null object
2 tname 77 non-null object
3 area 77 non-null float64
4 geometry 77 non-null geometry
dtypes: float64(1), geometry(1), object(3)
memory usage: 3.1+ KB
จะเห็นว่าตารางเป็น class geopandas.geodataframe.GeoDataFrame
ซึ่งแตกต่างกับ class pandas.core.frame.DataFrame
ตามปกติด้านบน
ทดสอบพลอตแผนที่จากตารางเบื้องต้น
fig = px.choropleth(
mapdf,
geojson=mapdf.geometry,
locations=mapdf.index,
color="area",
hover_data=["tname", "area"])
fig.update_geos(fitbounds="locations")
3) รวมตาราง
นำตารางที่มีค่าที่ต้องการ มารวมกับตารางแผนที่ โดยใช้คำสั่ง .merge
ซึ่ง left_on
และ right_on
ใช้ระบุคอลัมน์ที่ต้องมีชื่อตรงกันในการรวมกัน
merge_df = mapdf.merge(isodf, left_on="tname", right_on="index")
merge_df
code | name | tname | area | geometry | index | province_of_isolation | |
---|---|---|---|---|---|---|---|
0 | 10 | Bangkok | กรุงเทพมหานคร | 1568.95 | POLYGON ((100.85537 13.69198, 100.71321 13.711... | กรุงเทพมหานคร | 144807 |
1 | 11 | Samut Prakan | สมุทรปราการ | 960.09 | POLYGON ((100.85537 13.69198, 100.94033 13.657... | สมุทรปราการ | 55918 |
2 | 12 | Nonthaburi | นนทบุรี | 633.25 | POLYGON ((100.35029 14.11371, 100.33659 14.060... | นนทบุรี | 31452 |
3 | 13 | Pathum Thani | ปทุมธานี | 1520.06 | POLYGON ((100.91242 14.21391, 100.90529 13.955... | ปทุมธานี | 17768 |
4 | 14 | Phra Nakhon Si Ayutthaya | พระนครศรีอยุธยา | 2534.07 | POLYGON ((100.59405 14.65593, 100.57436 14.589... | พระนครศรีอยุธยา | 12302 |
... | ... | ... | ... | ... | ... | ... | ... |
72 | 92 | Trang | ตรัง | 4882.19 | MULTIPOLYGON (((99.44581 7.25092, 99.45387 7.2... | ตรัง | 11092 |
73 | 93 | Phatthalung | พัทลุง | 3764.62 | POLYGON ((100.12963 7.80172, 100.20086 7.77233... | พัทลุง | 11788 |
74 | 94 | Pattani | ปัตตานี | 1953.05 | POLYGON ((101.72350 6.57438, 101.67837 6.57712... | ปัตตานี | 17412 |
75 | 95 | Yala | ยะลา | 4482.17 | POLYGON ((101.60169 6.55539, 101.61321 6.53350... | ยะลา | 12281 |
76 | 96 | Narathiwat | นราธิวาส | 4483.92 | POLYGON ((101.72350 6.57438, 101.80836 6.46454... | นราธิวาส | 9454 |
77 rows × 7 columns
merge_df.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
Int64Index: 77 entries, 0 to 76
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 code 77 non-null object
1 name 77 non-null object
2 tname 77 non-null object
3 area 77 non-null float64
4 geometry 77 non-null geometry
5 index 77 non-null object
6 province_of_isolation 77 non-null int64
dtypes: float64(1), geometry(1), int64(1), object(4)
memory usage: 4.8+ KB
จะได้ตารางแบบ geopandas.geodataframe.GeoDataFrame
ที่มีคอลัมน์ province_of_isolation
ตามที่ต้องการ สามารถนำไปพลอตต่อได้เลย
fig = px.choropleth(
merge_df,
geojson=merge_df.geometry,
locations=merge_df.index,
color="province_of_isolation",
hover_data=["tname", "province_of_isolation"])
fig.update_geos(fitbounds="locations")