roberthsu2003 / __2024_01_28_sunday__

20 stars 2 forks source link

請依據lesson18_4.ipynb,建立新欄位_鄉鎮區 #12

Open roberthsu2003 opened 7 months ago

roberthsu2003 commented 7 months ago
截圖 2024-03-31 下午4 55 04
chiehyun2024 commented 7 months ago
import pandas as pd

df1 =pd.read_csv('各鄉鎮市區人口密度.csv')
df2 = df1.rename(columns={'statistic_yyy':'統計年','site_id':'區域別','people_total':'年底人口數','area':'土地面積','population_density':'人口密度'})
df3 = df2.drop(0)
df4 = df3.reindex(index=range(1,371))

def county(value:str) -> str:
    if len(value) > 4:
        return value[:3]
    else:
        return value

df4['縣市'] = df4['區域別'].map(county)

def township(value:str) -> str:
    if len(value) > 4:
        return value[-3:]
    else:
        return value

df4['鄉鎮區'] = df4['區域別'].map(township)
df4