pandas使用技巧【4】如何為DataFrame重命名列

簡(jiǎn)介: 本文主要介紹怎樣為DataFrame的columns重命名。

DataFrame

  1. 部分重命名
    columns = dict,使用字典類型的數(shù)據(jù)對(duì)列進(jìn)行重命名。
dataframe.rename(columns = {"old_name": "new_name"})
dataframe.rename(columns = {"old1": "new1", "old2":"new2"},  inplace=True)
  1. 全部重命名
    columns = new_columns,新列名的長(zhǎng)度必須與舊列名一致。
new_col = ['new1', 'new2',... , 'newn']
dataframe.columns = new_col
  1. 讀取文件的時(shí)候重命名
    names = new_col,可以在讀取文件的時(shí)候,給出新列名。
pd.read_csv('data', names = new_col, header=0)
  1. 使用str
    可以參考str小結(jié)。
dataframe.columns = dataframe.columns.str.replace('' '', ''_'')

附上小哥哥的視頻鏈接Data analysis in Python with pandas
Youtube ??
嗶哩嗶哩 ??

所有文章列表
pandas使用技巧總覽

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容