Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

replace multiple values in pandas column

df = pd.DataFrame({'a':['Small', 'Medium', 'High']})

In [22]: df
Out[22]: 
        a
0   Small
1  Medium
2    High

[3 rows x 1 columns]

df.replace({'a' : { 'Medium' : 2, 'Small' : 1, 'High' : 3 }})
Comment

pandas change column value based on multiple condition

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5, 2]

df['points'] = np.select(conditions, choices, default=0)
Comment

pandas changing multiple values in a column

mapping_dict = {
    'Android': 'Android',
    'Chrome OS': 'Chrome OS',
    'Linux': 'Linux',
    'Mac OS': 'macOS',
    'No OS': 'No OS',
    'Windows': 'Windows',
    'macOS': 'macOS'
}

laptops['os'] = laptops['os'].map(mapping_dict)
print(my_series)
Comment

replace multiple column values pandas

import pandas as pd

df = pd.DataFrame([
	[4, -9, 8],
	[1, 2, -4],
    [2, 2, -8],
    [0, 7, -4],
	[2, 5, 1]],
	columns=['a', 'b', 'c'])

df = df.replace({'a':{1:11, 2:22}, 'b':{5:55, 2:22}})
print(df)
Comment

pandas replace multiple values in column

# Specify Patterns to remove
removal_list = '|'.join(['
','
','	','
','>','<'])
# Replace each occurance with nothing ('') and replace the column
df['columnA'] = df['columnA'].str.replace(removal_list,'')
Comment

pandas .replace multiple values in column

df['column name'] = df['column name'].replace(['1st old value','2nd old value',...],['1st new value','2nd new value',...])
Comment

PREVIOUS NEXT
Code Example
Python :: read an excel file 
Python :: python slice 
Python :: dataframe check for nan in iterrows 
Python :: find keys to minimum value in dict 
Python :: how to define a class in python 
Python :: enumerate items python 
Python :: regex find email address in string python 
Python :: how get 1st column in all rows of a 2d matrix in python 
Python :: how to load user from jwt token request django 
Python :: python regular expressions 
Python :: python scipy moving average 
Python :: pytest temp directory 
Python :: getting input in python 
Python :: get method in python 
Python :: purpose of meta class in django 
Python :: convert str to datetime 
Python :: print animation python 
Python :: map two csv files python 
Python :: indexing python first and last 
Python :: pygame make a window 
Python :: python synonym library 
Python :: #finding the similarity among two sets 
Python :: python enumerate unique values 
Python :: python excel file 
Python :: abstract class python 
Python :: size of set python 
Python :: variables and data types in python 
Python :: download unsplash images script 
Python :: find the last point of line geopanda 
Python :: change python version in colab 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =