Tshering Tashi
Tshering Tashi

Reputation: 27

Pandas dataframe with calculated values

commission = 2%,
growth = 10%,
start_price = 100,
current_transaction = 1000

I wanted to create columns (transaction, revenue)

My question is how to create a dataframe with above information for 3 years.

Element Year 1 Year 2 Year 3
Transaction 1100 1210 1331
Revenue 2200 2420 2662

formula for transaction (transaction X growth) e.g(1000 X 10%)

formula for revenue (transaction X commission X price) e.g(1100X 2% X 100)

the year 2 column is outcome of year 1 ..so on.

Upvotes: 1

Views: 49

Answers (1)

Code Different
Code Different

Reputation: 93181

This is a classic exponential growth series. You can leverage numpy broadcasting to compute them:

# Convert all percentages into decimals
commission = 0.02
growth = 0.1
start_price = 100
current_transaction = 1000

# We are going to rely on numpy broadcasting to help us do computations on
# arrays
year = np.arange(1,4)
factor = (1 + growth) ** year

# And make the dataframe
df = pd.DataFrame({
    "Transaction": current_transaction * factor,
    "Revenue": current_transaction * factor * commission * start_price
}, index=year).T.add_prefix("Year ")

Upvotes: 1

Related Questions