问题:在pandas DataFrame中更改特定的列名称
我一直在寻找一种优雅的方法来更改中的指定列名称DataFrame
。
播放数据…
import pandas as pd
d = {
'one': [1, 2, 3, 4, 5],
'two': [9, 8, 7, 6, 5],
'three': ['a', 'b', 'c', 'd', 'e']
}
df = pd.DataFrame(d)
到目前为止,我发现的最优雅的解决方案…
names = df.columns.tolist()
names[names.index('two')] = 'new_name'
df.columns = names
我希望有一个简单的单线…此尝试失败了…
df.columns[df.columns.tolist().index('one')] = 'another_name'
非常感谢收到的任何提示。
I was looking for an elegant way to change a specified column name in a DataFrame
.
play data …
import pandas as pd
d = {
'one': [1, 2, 3, 4, 5],
'two': [9, 8, 7, 6, 5],
'three': ['a', 'b', 'c', 'd', 'e']
}
df = pd.DataFrame(d)
The most elegant solution I have found so far …
names = df.columns.tolist()
names[names.index('two')] = 'new_name'
df.columns = names
I was hoping for a simple one-liner … this attempt failed …
df.columns[df.columns.tolist().index('one')] = 'another_name'
Any hints gratefully received.
回答 0
确实存在一行代码:
In [27]: df=df.rename(columns = {'two':'new_name'})
In [28]: df
Out[28]:
one three new_name
0 1 a 9
1 2 b 8
2 3 c 7
3 4 d 6
4 5 e 5
以下是该rename
方法的文档字符串。
定义:df.rename(self,index = None,columns = None,copy = True,inplace = False)
Docstring:
使用输入功能更改索引和/或列或
功能。函数/字典值必须唯一(1对1)。标签不行
dict / Series中包含的内容将保持不变。
参量
----------
index:类似dict或函数,可选
转换以应用于索引值
列:类似字典或函数,可选
转换以应用于列值
复制:布尔值,默认为True
同时复制基础数据
inplace:布尔值,默认为False
是否返回新的DataFrame。如果为True,则复制值为
忽略了。
也可以看看
--------
Series.rename
退货
-------
重命名为:DataFrame(新对象)
A one liner does exist:
In [27]: df=df.rename(columns = {'two':'new_name'})
In [28]: df
Out[28]:
one three new_name
0 1 a 9
1 2 b 8
2 3 c 7
3 4 d 6
4 5 e 5
Following is the docstring for the rename
method.
Definition: df.rename(self, index=None, columns=None, copy=True, inplace=False)
Docstring:
Alter index and / or columns using input function or
functions. Function / dict values must be unique (1-to-1). Labels not
contained in a dict / Series will be left as-is.
Parameters
----------
index : dict-like or function, optional
Transformation to apply to index values
columns : dict-like or function, optional
Transformation to apply to column values
copy : boolean, default True
Also copy underlying data
inplace : boolean, default False
Whether to return a new DataFrame. If True then value of copy is
ignored.
See also
--------
Series.rename
Returns
-------
renamed : DataFrame (new object)
回答 1
由于inplace
argument是可用的,因此您无需复制原始数据帧并将其分配回自身,而是执行以下操作:
df.rename(columns={'two':'new_name'}, inplace=True)
Since inplace
argument is available, you don’t need to copy and assign the original data frame back to itself, but do as follows:
df.rename(columns={'two':'new_name'}, inplace=True)
回答 2
关于什么?
df.columns.values[2] = "new_name"
What about?
df.columns.values[2] = "new_name"
回答 3
熊猫0.21现在具有轴参数
重命名方法已获得一个axis参数,以匹配其余大多数熊猫API。
因此,除此以外:
df.rename(columns = {'two':'new_name'})
你可以做:
df.rename({'two':'new_name'}, axis=1)
要么
df.rename({'two':'new_name'}, axis='columns')
Pandas 0.21 now has an axis parameter
The rename method has gained an axis parameter to match most of the rest of the pandas API.
So, in addition to this:
df.rename(columns = {'two':'new_name'})
You can do:
df.rename({'two':'new_name'}, axis=1)
or
df.rename({'two':'new_name'}, axis='columns')
回答 4
If you know which column # it is (first / second / nth) then this solution posted on a similar question works regardless of whether it is named or unnamed, and in one line: https://stackoverflow.com/a/26336314/4355695
df.rename(columns = {list(df)[1]:'new_name'}, inplace=True)
# 1 is for second column (0,1,2..)
回答 5
要重命名列,这里是一种简单的方法,它既Default(0,1,2,etc;)
适用于现有的列,也适用于现有的列,但对于较大的数据集(具有许多列)而言,用处不大。
对于更大的数据集,我们可以切片所需的列并应用以下代码:
df.columns = ['new_name','new_name1','old_name']
For renaming the columns here is the simple one which will work for both Default(0,1,2,etc;)
and existing columns but not much useful for a larger data sets(having many columns).
For a larger data set we can slice the columns that we need and apply the below code:
df.columns = ['new_name','new_name1','old_name']
回答 6
以下简短代码可以帮助您:
df3 = df3.rename(columns={c: c.replace(' ', '') for c in df3.columns})
从列中删除空格。
Following short code can help:
df3 = df3.rename(columns={c: c.replace(' ', '') for c in df3.columns})
Remove spaces from columns.
回答 7
熊猫0.23.4版
df.rename(index=str,columns={'old_name':'new_name'},inplace=True)
作为记录:
省略index = str将给出错误替换,带有意外参数’columns’
pandas version 0.23.4
df.rename(index=str,columns={'old_name':'new_name'},inplace=True)
For the record:
omitting index=str will give error replace has an unexpected argument
‘columns’
回答 8
另一种选择是简单地复制和删除列:
df = pd.DataFrame(d)
df['new_name'] = df['two']
df = df.drop('two', axis=1)
df.head()
之后,您将得到结果:
one three new_name
0 1 a 9
1 2 b 8
2 3 c 7
3 4 d 6
4 5 e 5
Another option would be to simply copy & drop the column:
df = pd.DataFrame(d)
df['new_name'] = df['two']
df = df.drop('two', axis=1)
df.head()
After that you get the result:
one three new_name
0 1 a 9
1 2 b 8
2 3 c 7
3 4 d 6
4 5 e 5