Rename specific column(s) in pandas
data.rename(columns={'gdp':'log(gdp)'}, inplace=True)
The rename
show that it accepts a dict as a param for columns
so you just pass a dict with a single entry.
Also see related
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)
Renaming specific columns of pandas dataframe
df.rename
by default returns the renamed DataFrame and leaves the original DataFrame unchanged. So you can either use
df.rename(..., inplace=True)
to do the column renames in place or reassign the returned, renamed DataFrame:
df = df.rename(...)
Rename specific columns in excel with pandas
Here is my try:
import pandas as pd
excelList = pd.read_excel("participantsList.xlsx", sheet_name="Table1")
#participatedList = open('participated.txt','r')
#CODE TO CHANGE THE PARAMETER
#excelList.to_excel(r'newList.xlsx')
data = pd.read_csv('participated.txt', sep=" ", header=None)
data.columns = ["First Name", "Last Name"]
newList = excelList.copy()
txt = 0
while txt < len(data):
i = 0
while i < len(excelList):
if data['Last Name'][txt] == excelList['Last Name'][i]:
newList.loc[i,'Participated'] = "Yes"
i += 1
txt += 1
newList.to_excel(r'newList.xlsx')
print(newList)
Pandas rename column by position?
try this
df.rename(columns={ df.columns[1]: "your value" }, inplace = True)
Rename column values using pandas DataFrame
If I am understanding your question correctly, you are trying to change the values in a column and not the column name itself.
Given you have mixed data type there, I assume that column is of type object
and thus the number is read as string.
df['col_name'] = df['col_name'].str.replace('G', '1')
How to rename columns in Pandas automatically?
You can use:
df.columns = df.columns.map(lambda x: f'column_{x+1}')
Example output:
column_1 column_2 column_3 column_4 column_5 column_6 column_7 column_8 column_9 column_10
0 0 1 2 3 4 5 6 7 8 9
Used input:
df = pd.DataFrame([range(10)])
Related Topics
How to Use a Multiprocessing.Manager()
Add Sum of Values of Two Lists into New List
Python Datetime Object Show Wrong Timezone Offset
How to Set the Absolute Position of Figure Windows with Matplotlib
What Can Multiprocessing and Dill Do Together
Removing Duplicates from Dictionary
If Range() Is a Generator in Python 3.3, Why How to Not Call Next() on a Range
Speed Comparison with Project Euler: C VS Python VS Erlang VS Haskell
Python: Platform Independent Way to Modify Path Environment Variable
Differences Between Numpy.Random and Random.Random in Python
What Does "Bound Method" Error Mean When I Call a Function
Nested Dictionary to Multiindex Dataframe Where Dictionary Keys Are Column Labels
How to Separate the Functions of a Class into Multiple Files