New Pandas column based on other columns

You can use the following methods to create new column based on values from other columns:

df['Total_Price'] = df.Price * df.Qty
df['Total_Price'] = df.apply(lambda row: row.Price * row.Qty, axis = 1)
df['Total_Price'] = np.multiply(df['Price'], df['Qty'])
df['Total_Price'] = np.vectorize(fx)(df['Price'], df['Qty'])

Lets create a DataFrame..

import pandas as pd import numpy as np df = pd.DataFrame() df['Item'] = ['Item-1', 'Item-2', 'Item-3','Item-4','Item-5','Item-6'] df['Price'] = [82, 38, 63,22,55,40] df['Qty'] = [4, 1, 4,3,3,2] df
Item Price Qty 0 Item-1 82 4 1 Item-2 38 1 2 Item-3 63 4 3 Item-4 22 3 4 Item-5 55 3 5 Item-6 40 2

Using simple DataFrame multiplication

df['Total_Price'] = df.Price * df.Qty df

dataframe multiplication
Item Price Qty Total_Price 0 Item-1 82 4 328 1 Item-2 38 1 38 2 Item-3 63 4 252 3 Item-4 22 3 66 4 Item-5 55 3 165 5 Item-6 40 2 80

Using df.apply()

df['Total_Price'] = df.apply(lambda row: row.Price * row.Qty, axis = 1) df
Item Price Qty Total_Price 0 Item-1 82 4 328 1 Item-2 38 1 38 2 Item-3 63 4 252 3 Item-4 22 3 66 4 Item-5 55 3 165 5 Item-6 40 2 80

Using np.multiply()

df['Total_Price'] = np.multiply(df['Price'], df['Qty']) df
Item Price Qty Total_Price 0 Item-1 82 4 328 1 Item-2 38 1 38 2 Item-3 63 4 252 3 Item-4 22 3 66 4 Item-5 55 3 165 5 Item-6 40 2 80

Using vectorize arbitrary function

def fx(x, y): return x*y df['Total_Price'] = np.vectorize(fx)(df['Price'], df['Qty']) df

vectorised xondition
Item Price Qty Total_Price 0 Item-1 82 4 328 1 Item-2 38 1 38 2 Item-3 63 4 252 3 Item-4 22 3 66 4 Item-5 55 3 165 5 Item-6 40 2 80