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 :: define variable with if statement python 
Python :: find angle mbc in python 
Python :: python set a specific datetime 
Python :: python install bigquery 
Python :: the system cannot find the file specified sublime text 3 python 
Python :: python code to open windows command prompt 
Python :: python merge two dictionaries 
Python :: python drop axis 
Python :: binary search algorithm python 
Python :: how to find the text inside button in tkinter 
Python :: what is my python working directory 
Python :: sort tuple list python 
Python :: connect flask with postgresql 
Python :: copy a file from one directroy to other using python 
Python :: mad scipy 
Python :: csv reader python skip header 
Python :: pandas inner join on two columns 
Python :: accuracy score 
Python :: discord.py cog 
Python :: count items in list 
Python :: python iterar diccionario 
Python :: NumPy flip Example 
Python :: python- find multiple values in a column 
Python :: django get or 404 
Python :: flask mail 
Python :: find the max value in dictionary python 
Python :: Python how to use __gt__ 
Python :: python typeddict 
Python :: colab pip 
Python :: screen size python 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =