Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas change column name from a dictionary

df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
cols= {"A": "a", "B": "c"}
>>> df.rename(columns=cols)
   a  c
0  1  4
1  2  5
2  3  6
Comment

pandas rename column values dictionary

df['col1'].map(di)       # note: if the dictionary does not exhaustively map all
                         # entries then non-matched entries are changed to NaNs
Comment

pandas rename column by dictionary

# import pandas
import pandas as pd
 
# create data frame
df = pd.DataFrame({'First Name': ["Mukul", "Rohan", "Mayank",
                                  "Vedansh", "Krishna"],
                    
                   'Location': ["Saharanpur", "Rampur", "Agra",
                                "Saharanpur", "Noida"],
                    
                   'Pay': [56000.0, 55000.0, 61000.0, 45000.0, 62000.0]})
 
# print original data frame
display(df)
 
# create a dictionary
# key = old name
# value = new name
dict = {'First Name': 'Name',
        'Location': 'City',
        'Pay': 'Salary'}
 
# call rename () method
df.rename(columns=dict,
          inplace=True)
 
# print Data frame after rename columns
display(df)
Comment

pandas rename column values dictionary

df['col1'].map(di).fillna(df['col1'])
Comment

PREVIOUS NEXT
Code Example
Python :: python pandas read csv from txt tab delimiter 
Python :: python alphabetical order 
Python :: python get desktop environment 
Python :: matplotlib different number of subplots 
Python :: how to get key value in nested dictionary python 
Python :: input in python 
Python :: how to send file in python request 
Python :: model evaluate function 
Python :: selenium set chrome executable path 
Python :: python print format 
Python :: basic script 
Python :: how to take multiple line inputs in python 
Python :: zip multiple lists 
Python :: python replace 
Python :: how to use dictionaries in python 
Python :: counter in python 
Python :: Python program to print negative numbers in a list 
Python :: python tkinter cursor types 
Python :: generate all combinatinosrs of a list pyton 
Python :: python how to see what pip packages are installed 
Python :: Pandas: How to Drop Rows that Contain a Specific String in 2 columns 
Python :: Python program to combine each line from first file with the corresponding line in second file 
Python :: add list to list python 
Python :: Display the data types of the DataFrame 
Python :: how to merge between two columns and make a new one in pandas dataframe 
Python :: extract text from pdf python 
Python :: termcolor print python 
Python :: np sum 
Python :: python run powershell command and get output 
Python :: lastindexof python 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =