Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas replace 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 :: how to compile python 
Python :: How to select rows in a DataFrame between two values, in Python Pandas? 
Python :: how to make a list using lambda function in python 
Python :: how to display csv in pandas 
Python :: split string by spaces python 
Python :: comment out a block in python 
Python :: create a blank image numpy 
Python :: how to get key of a particular value in dictionary python using index 
Python :: colorbar font size python 
Python :: delete virtual environment in python windows 
Python :: how to save an image with the same name after editing in python pillow module 
Python :: make a white image numpy 
Python :: python - remove floating in a dataframe 
Python :: difference between two dictionaries python 
Python :: get number of zero is a row pandas 
Python :: cannot convert float NaN to integer 
Python :: how to check the size of a file in python 
Python :: Python remove punctuation from a string 
Python :: install python altair 
Python :: pandas sort dataframe by column 
Python :: how to add two numbers 
Python :: get dataframe column names 
Python :: check if a list contains any item from another list python 
Python :: python save dictionary 
Python :: children beautiful soup 
Python :: data structures and algorithms in python 
Python :: pandas df num rows 
Python :: python del 
Python :: Python NumPy repeat Function Syntax 
Python :: count different values in list python 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =