简体   繁体   English

如何在不引用旧列名和不创建新数据框的情况下更改列名?

[英]How can I change column names without referencing old column names and without creating a new data frame?

I know I can change column names by doing:我知道我可以通过执行以下操作来更改列名:

df = df.rename(columns = {old_name: new_name})

But I have to rename a lot and the old names are long so I wanna just pass in a list for the new names.但是我必须重命名很多,并且旧名称很长,所以我只想传递新名称的列表。 If I make the new columns its own data frame this works fine:如果我使新列成为自己的数据框,这可以正常工作:

ranks = sw[sw.columns[9:15]]
ranks.columns = ['ranking_1', 'ranking_2', 'ranking_3', 'ranking_4', 'ranking_5', 'ranking_6']

However if I try to just do it with the slice of columns without changing it to its own df, nothing happens:但是,如果我尝试仅使用列切片而不将其更改为自己的 df,则不会发生任何事情:

sw[sw.columns[9:15]].columns = ['ranking_1', 'ranking_2', 'ranking_3', 'ranking_4', 'ranking_5', 'ranking_6']

How can I rename the columns and keep them in the original data frame?如何重命名列并将它们保留在原始数据框中?

This:这个:

sw[sw.columns[9:15]] = ['ranking_1', 'ranking_2', 'ranking_3', 'ranking_4', 'ranking_5', 'ranking_6']

changes the data of your sw , not the column names.更改sw的数据,而不是列名。 A manual way to change the column names can be:手动更改列名的方法可以是:

# new column names
new_cols = ['ranking_1', 'ranking_2', 'ranking_3', 'ranking_4', 'ranking_5', 'ranking_6']

# slice the columns name, replace the part you want and concatenate
sw.columns = list(sw.columns[:9]) + new_cols + list(sw.columns[15:])

Or you can also build the dictionary and use rename :或者您也可以构建字典并使用rename

rename_dict = {a:b for a,b in zip(new_cols, sw.columns[9:15])}
sw.rename(columns=rename_dict)

Do you mean something like this?你的意思是这样的吗?

import pandas as pd

df = pd.DataFrame(columns=['A','B','C','D','E',
                           'F','G','H','I','J',
                           'K','L','M','N','O'])
col = list(df.columns)                # get them all
rankcol = ['ranking_1', 'ranking_2', 'ranking_3', 
           'ranking_4', 'ranking_5', 'ranking_6']

df.columns = col[:9]+rankcol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 Pandas 数据框的情况下将列名添加到 ipysheet? - How to add column names to ipysheet without using pandas data frame? 将新的Pandas DataFrame附加到旧的,而没有对列名称进行排序的情况 - Append new Pandas DataFrame to an old one without column names sorted 根据列名过滤数据框,而不使用pandas中的索引 - Filter the data frame based on the column names without using index in pandas 如何在不知道列名的情况下重新组织和分离数据框中的数据? - How can I reorganize and separate data in a dataframe without knowing the column names? Python - Pandas - 将列名复制到新的 dataframe 而不带数据 - Python - Pandas - Copy column names to new dataframe without bringing data 按列名创建新列 - Creating new column by column names 连接数据框中的值和列名以创建新的数据框 - Concatenate values and column names in a data frame to create a new data frame 从数据框中提取相对于其他数据框中的bin值的行(不使用列名) - Extracting rows from a data frame with respect to the bin value from other data frame(without using column names) 通过从 python 中的数据框中引用其他列来创建新列 - Creating a new column by referencing other columns from a data frame in python "python - 如何使用pymysql在python中选择没有列名?" - How to select without column names in python with pymysql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM