簡介: 本文主要介紹怎樣為DataFrame的columns重命名。
DataFrame
- 部分重命名
columns = dict,使用字典類型的數據對列進行重命名。
dataframe.rename(columns = {"old_name": "new_name"})
dataframe.rename(columns = {"old1": "new1", "old2":"new2"}, inplace=True)
- 全部重命名
columns = new_columns,新列名的長度必須與舊列名一致。
new_col = ['new1', 'new2',... , 'newn']
dataframe.columns = new_col
- 讀取文件的時候重命名
names = new_col,可以在讀取文件的時候,給出新列名。
pd.read_csv('data', names = new_col, header=0)
- 使用str
可以參考str小結。
dataframe.columns = dataframe.columns.str.replace('' '', ''_'')
附上小哥哥的視頻鏈接Data analysis in Python with pandas
Youtube ??
嗶哩嗶哩 ??
所有文章列表
pandas使用技巧總覽