Conditionally fill column values based on another columns value in pandas Conditionally fill column values based on another columns value in pandas python python

Conditionally fill column values based on another columns value in pandas


You probably want to do

df['Normalized'] = np.where(df['Currency'] == '$', df['Budget'] * 0.78125, df['Budget'])


Similar results via an alternate style might be to write a function that performs the operation you want on a row, using row['fieldname'] syntax to access individual values/columns, and then perform a DataFrame.apply method upon it

This echoes the answer to the question linked here: pandas create new column based on values from other columns

def normalise_row(row):    if row['Currency'] == '$'    ...    ...    ...    return resultdf['Normalized'] = df.apply(lambda row : normalise_row(row), axis=1) 


An option that doesn't require an additional import for numpy:

df['Normalized'] = df['Budget'].where(df['Currency']=='$', df['Budget'] * 0.78125)