Renaming column names in Pandas
Just assign it to the .columns
attribute:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
Changing a specific column name in pandas 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)
How to change column names in pandas Dataframe using a list of names?
you could use
df.columns = ['Leader', 'Time', 'Score']
Pandas rename column by position?
try this
df.rename(columns={ df.columns[1]: "your value" }, inplace = True)
Related Topics
Adding Some Space Between the X-Axis and the Bars, in Ggplot
Convert Categorical Variables to Numeric in R
How to Select Variables in an R Dataframe Whose Names Contain a Particular String
How to Add a Diagonal Line to a Plot
Gsub a Every Element After a Keyword in R
Change the Class from Factor to Numeric of Many Columns in a Data Frame
Convert Dataframe Column to 1 or 0 for "True"/"False" Values and Assign to Dataframe
Loop Through Data Frame and Variable Names
Using Ifelse Statement on the Whole Dataset Instead of a Single Column
Remove Unwanted Symbols from Expression Function - R
R - Getting Characters After Symbol
Calculate Row Means on Subset of Columns
Force the Origin to Start At 0
Why Can't R'S Ifelse Statements Return Vectors
Error: C Stack Usage Is Too Close to the Limit
Create a Sequential Number (Counter) For Rows Within Each Group of a Dataframe
Why Does Data.Table Update Names(Dt) by Reference, Even If I Assign to Another Variable