Skip to main content
 首页 » 编程设计

python之重命名 Pandas 中的选定列

2024年11月24日10daizhj

<分区>

我正在尝试使用 iloc 和 df.columns 函数重命名我的数据框中的选定列(比如两个 las 列),但它似乎不起作用对我来说,我不知道为什么。这是我想要实现的玩具示例:

import pandas as pd 
 
d = {'one': list(range(5)), 
     'two': list(range(5)), 
     'three': list(range(5)), 
     'four': list(range(5)), 
     'five': ['a', 'b', 'c', 'd', 'e'], 
     'six': ['a', 'b', 'c', 'd', 'e'] 
    } 
df = pd.DataFrame(d) 
 
df.iloc[:,-2:].columns = ['letter_1','letter_2'] 
 
df.columns 

但我一直在获取原始列的名称:

Index(['one', 'two', 'three', 'four', 'five', 'six'], dtype='object') 

我错过了什么?