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)
Taking Tom Kimber's suggestion one step further, you could use a Function Dictionary to set various conditions for your functions. This solution is expanding the scope of the question.
I'm using an example from a personal application.
# write the dictionarydef applyCalculateSpend (df_name, cost_method_col, metric_col, rate_col, total_planned_col): calculations = { 'CPMV' : df_name[metric_col] / 1000 * df_name[rate_col], 'Free' : 0 } df_method = df_name[cost_method_col] return calculations.get(df_method, "not in dict")# call the function inside a lambdatest_df['spend'] = test_df.apply(lambda row: applyCalculateSpend(row,cost_method_col='cost method',metric_col='metric',rate_col='rate',total_planned_col='total planned'), axis = 1) cost method metric rate total planned spend0 CPMV 2000 100 1000 200.01 CPMV 4000 100 1000 400.04 Free 1 2 3 0.0