I was looking for an elegant way to change a specified column name in a DataFrame
.
我正在寻找一种优雅的方式来更改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.
任何暗示感激地接受。
7 个解决方案
#1
252
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.
下面是重命名方法的docstring。
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)
#2
85
Since inplace
argument is available, you don't need to copy and assign the original data frame back to itself, but do as follows:
由于inplace参数是可用的,您不需要将原始数据帧复制并分配给它自己,但是要做以下操作:
df.rename(columns={'two':'new_name'}, inplace=True)
#3
21
What about?
关于什么?
df.columns.values[2] = "new_name"
#4
2
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.
重命名方法获得了一个与其他大多数熊猫API匹配的axis参数。
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')
#5
2
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).
对于重命名列,这里有一个简单的方法,它既适用于默认值(0,1,2,等等),也适用于现有列,但对于较大的数据集(有许多列)来说用处不大。
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
0
Following short code can help:
以下简短的代码可以帮助:
df3 = df3.rename(columns={c: c.replace(' ', '') for c in df3.columns})
Remove spaces from columns.
从列删除空间。
#7
0
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://*.com/a/26336314/4355695
如果您知道它是哪一列(第1 /2 / nth),那么这个发布在类似问题上的解决方案不管它是命名的还是未命名的,都可以工作,在一行中:https://*.com/a/26336314/4355695
df.rename(columns = {list(df)[1]:'new_name'}, inplace=True)
# 1 is for second column (0,1,2..)
#1
252
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.
下面是重命名方法的docstring。
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)
#2
85
Since inplace
argument is available, you don't need to copy and assign the original data frame back to itself, but do as follows:
由于inplace参数是可用的,您不需要将原始数据帧复制并分配给它自己,但是要做以下操作:
df.rename(columns={'two':'new_name'}, inplace=True)
#3
21
What about?
关于什么?
df.columns.values[2] = "new_name"
#4
2
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.
重命名方法获得了一个与其他大多数熊猫API匹配的axis参数。
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')
#5
2
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).
对于重命名列,这里有一个简单的方法,它既适用于默认值(0,1,2,等等),也适用于现有列,但对于较大的数据集(有许多列)来说用处不大。
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
0
Following short code can help:
以下简短的代码可以帮助:
df3 = df3.rename(columns={c: c.replace(' ', '') for c in df3.columns})
Remove spaces from columns.
从列删除空间。
#7
0
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://*.com/a/26336314/4355695
如果您知道它是哪一列(第1 /2 / nth),那么这个发布在类似问题上的解决方案不管它是命名的还是未命名的,都可以工作,在一行中:https://*.com/a/26336314/4355695
df.rename(columns = {list(df)[1]:'new_name'}, inplace=True)
# 1 is for second column (0,1,2..)