xxxxxxxxxx
energy['Country'] = energy['Country'].replace(['Afghanistan','Albania'],['Sa','lol'])
xxxxxxxxxx
# Replace missing values
df["col"].replace(np.nan, new_val)
# Replace other values
df["col"].str.replace('old','new')
# Replace using masking
df.loc[df['col']=='old', 'col'] = 'new'
# Replace all specified value in the dataframe
df_replaced = df.replace(1, 0)
xxxxxxxxxx
# change value in column_where_to_change and in the row where column_name == column_value
df.loc[df['<column_name>']=='column_value', '<column_where_to_change>'] = '<new_value>'
xxxxxxxxxx
# Importing the libraries
import pandas as pd
import numpy as np
# data
Student = {
'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'],
'gender': ['male', 'male', 'male', 'female', 'female', 'male'],
'math score': [50, 100, 70, 80, 75, 40],
'test preparation': ['none', 'completed', 'none', 'completed',
'completed', 'none'],
}
# creating a Dataframe object
df = pd.DataFrame(Student)
# Applying the condition
df.loc[df["gender"] == "male", "gender"] = 1