python – 从列表中只替换数据框中的几个标题
发布时间:2020-12-20 11:06:16 所属栏目:Python 来源:网络整理
导读:我有一个数据框,我想更改最后30个标题.我有一个需要更改的值列表,但我想保留数据帧中原来的前两个标题.例如,我的数据帧类似 Customer ID Email Topwater ... Plastics Finesse12345 me@me.com 1 ... 1 0... 我的名单是: [Bait #1,Bait #2,Bait #3,...,Bait
我有一个数据框,我想更改最后30个标题.我有一个需要更改的值列表,但我想保留数据帧中原来的前两个标题.例如,我的数据帧类似
Customer ID Email Topwater ... Plastics Finesse 12345 me@me.com 1 ... 1 0 ... 我的名单是: [Bait #1,Bait #2,Bait #3,...,Bait #10,Bait #11] 我正在寻找这个: Customer ID Email Bait#1 ... Bait #10 Bait #11 12345 me@me.com 1 ... 1 0 ... 我试过这个(其中df_binary是带有我想要改变的标题的数据帧,但它似乎没有做任何事情,只返回初始数据帧: header_list = ['Customer ID','Email'] header_list.extend(list_of_baits) df_binary.iloc[:,2:37].columns = my_list2 解决方法
我认为需要替换最后3个值 – 转换所有列名称而不是最后列出并添加新项目:
print (df) Customer ID Email Topwater ... Plastics Finesse 0 12345 me@me.com 1 ... 1 0 4 list_of_baits = ['Bait #1','Bait #2','Bait #3'] #for last 30 change -3 to -30 df.columns = df.columns[:-3].tolist() + list_of_baits print (df) Customer ID Email Topwater Bait #1 Bait #2 Bait #3 0 12345 me@me.com 1 ... 1 0 4 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |