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

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

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
Related Topics
- Pandas DataFrame: GroupBy Examples
- Pandas DataFrame Aggregation and Grouping
- How to Sort Pandas DataFrame
- Pandas DataFrame: query() function
- Finding and removing duplicate rows in Pandas DataFrame
- How to Replace NaN Values With Zeros in Pandas DataFrame
- How to read CSV File using Pandas DataFrame.read_csv()
- How to Convert Pandas DataFrame to NumPy Array
- How to shuffle a DataFrame rows
- Import multiple csv files into one pandas DataFrame
- New Pandas dataframe column based on if-else condition
- How to Convert a Dictionary to Pandas DataFrame
- Rename Pandas columns/index names (labels)