Open roberthsu2003 opened 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