You’re given two dataframes. One contains information about addresses and the other contains relationships between various cities and states.
Write a function complete_address
to create a single dataframe with complete addresses in the format of street, city, state, zip code.
str.split( )
依字串拆分df_addresses
和 df_cities
依相同欄位 city 合併【Day 17|資料合併的三種常用語法】import pandas as pd
def complete_address(df_addresses: pd.DataFrame, df_cities: pd.DataFrame):
# spilt the address 拆分adress
df_addresses[['street','city','zip']] = df_addresses['address'].str.split(',',expand=True)
# check the space 檢查並刪除空白格
df_addresses['city'] = df_addresses['city'].str.strip()
df_addresses['zip'] = df_addresses['zip'].str.strip()
# merge two dataframe 關聯合併
new = pd.merge(df_addresses,df_cities,on='city')
# get the complete addresses 組合所需資料
new['address'] = new['street'] +', '+ new['city'] +', '+ new['state'] +', '+ new['zip']
new.drop(columns=['street','city','zip','state'],inplace=True)
return new
Pandas 中的 str.split( pat = '分割符號', expand = True 或 False )
操作目的在於「在指定分隔符號依字串拆分」,當資料內容冗長,需要切分閱讀或重新組合時,可採取該用法!
嗨,我是 Eva,一位正在努力跨進資料科學領域的女子!
這道屬於 Interview Query 中 Pandas Medium 難度的題目,需熟悉分割操作,礙於之前著重在其他操作語法,解題時多花了時間研究 str.split()
的使用方式,很開心又多紀錄一個常用的操作!大家不妨也挑戰看看吧!如果有任何不理解、錯誤或其他方法想分享的話,歡迎留言!喜歡的話,也歡迎按讚訂閱唷!我們明天見!