更改多个列名但不是全部 - Pandas Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38101009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing multiple column names but not all of them - Pandas Python
提问by Antonio López Ruiz
I would like to know if there is a function to change specific column names but without selecting a specific name or without changing all of them.
我想知道是否有更改特定列名但不选择特定名称或不更改所有列名的功能。
I have the code:
我有代码:
df=df.rename(columns = {'nameofacolumn':'newname'})
But with it i have to manually change each one of them writing each name. Also to change all of them I have
但是有了它,我必须手动更改每个人写的每个名字。还要改变我所有的
df = df.columns['name1','name2','etc']
I would like to have a function to change columns 1 and 3 without writing their names just stating their location. Thanks!
我想要一个函数来更改第 1 列和第 3 列,而无需写下他们的名字,只是说明他们的位置。谢谢!
回答by mgoldwasser
say you have a dictionary of the new column names and the name of the column they should replace:
假设您有一个包含新列名和它们应该替换的列名的字典:
df.rename(columns={'old_col':'new_col', 'old_col_2':'new_col_2'}, inplace=True)
But, if you don't have that, and you only have the indices, you can do this:
但是,如果你没有那个,而你只有索引,你可以这样做:
column_indices = [1,4,5,6]
new_names = ['a','b','c','d']
old_names = df.columns[column_indices]
df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
回答by EdChum
You can use a dict
comprehension and pass this to rename
:
您可以使用dict
理解并将其传递给rename
:
In [246]:
df = pd.DataFrame(columns=list('abc'))
new_cols=['d','e']
df.rename(columns=dict(zip(df.columns[1:], new_cols)),inplace=True)
df
Out[246]:
Empty DataFrame
Columns: [a, d, e]
Index: []
It also works if you pass a list of ordinal positions:
如果您传递顺序位置列表,它也有效:
df.rename(columns=dict(zip(df.columns[[1,2]], new_cols)),inplace=True)
回答by StefanK
You don't need to use rename method at all.
您根本不需要使用重命名方法。
You simply replace the old column names with new ones using lists. To rename columns 1 and 3 (with index 0 and 2), you do something like this:
您只需使用列表将旧列名替换为新列名。要重命名第 1 列和第 3 列(索引为 0 和 2),请执行以下操作:
df.columns.values[[0, 2]] = ['newname0', 'newname2']
df.columns.values[[0, 2]] = ['newname0', 'newname2']
or possibly if you are using older version of pandas than 0.16.0, you do:
或者如果您使用的是比 0.16.0 旧版本的熊猫,您可以:
df.keys().values[[0, 2]] = ['newname0', 'newname2']
df.keys().values[[0, 2]] = ['newname0', 'newname2']
The advantage of this approach is, that you don't need to copy the whole dataframe with syntax df = df.rename
, you just change the index values.
这种方法的优点是,您不需要使用 syntax 复制整个数据帧df = df.rename
,您只需更改索引值。
回答by HakunaMaData
You should be able to reference the columns by index using ..df.columns[index]
您应该能够使用 ..df.columns[index] 通过索引引用列
>> temp = pd.DataFrame(np.random.randn(10, 5),columns=['a', 'b', 'c', 'd', 'e'])
>> print(temp.columns[0])
a
>> print(temp.columns[1])
b
So to change the value of specific columns, first assign the values to an array and change only the values you want
因此,要更改特定列的值,首先将值分配给数组并仅更改您想要的值
>> newcolumns=temp.columns.values
>> newcolumns[0] = 'New_a'
Assign the new array back to the columns and you'll have what you need
将新数组分配回列,您将拥有所需的内容
>> temp.columns = newcolumns
>> temp.columns
>> print(temp.columns[0])
New_a