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 :: write number of lines in file python 
Python :: convert_text_to_hexadecimal_viva.py in python 
Python :: how to import data from csv to jupyter notebook 
Python :: random list python 
Python :: python print do not use scientific notation 
Python :: opencv skip video frames 
Python :: remove item from list if it exists python 
Python :: python dataclass default factory 
Python :: execute python in notepad++ 
Python :: how to make a kivy label multiline text 
Python :: python check if type 
Python :: install python selenium webdriver 
Python :: find python path cmd 
Python :: handler.setLevel(logging.DEBUG) not working python 
Python :: median absolute deviation python 
Python :: sort defaultdict by value 
Python :: parse first characters from string python 
Python :: except as exception: 
Python :: openpyxl xls 
Python :: django unique_together 
Python :: pandas iterate columns 
Python :: discord.py get profile picture 
Python :: breaking big csv into chunks pandas 
Python :: how to save unzipped files in python 
Python :: pop vs remove python 
Python :: append element to an array python 
Python :: __gt__ 
Python :: django model current timestamp 
Python :: python dataframe shape 
Python :: python how to open a file in a different directory in mac 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =