Hi I'm a new user for python / Pandas. Please help me with Pandas.DataFrame (and perhaps apply methods). I have two DataFrames, examples as follows. I would like to create a new column df_products['Sales Revenue'] by looking up the unit price of each product, times the df_products['Qty Sold']. How do I do that properly? Thank you.
# Pandas DataFrame example questions
import pandas as pd
products = ({
'Product': ['A', 'B', 'C', 'D'],
'Qty Sold': [10, 15, 22, 40]
})
prices =({
'Product': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'Unit Price': [3.20, 3.40, 2.20, 11.00, 8.05, 9.30, 4.00]
})
df_products = pd.DataFrame(product)
df_prices = pd.DataFrame(prices)
df_products
**Product** **Qty Sold** **Sales Revenue**(to create)
0 A 10 =10*3.20
1 B 15 =15*3.40
2 C 22 ........
3 D 40 ........
df_prices
Product Unit Price
0 A 3.20
1 B 3.40
2 C 2.20
3 D 11.00
4 E 8.05
5 F 9.30
6 G 4.00
CodePudding user response:
df_products['Sales Revenue'] = df_products['Qty Sold'].mul(df_prices['Unit Price'])
CodePudding user response:
df_merged = df_products.merge(df_prices, on = 'Product')
df_products['Sales Revenue'] = df_merged['Qty Sold'] * df_merged['Unit Price']
I thinks that is not bad way but maybe better is possible.