0
$\begingroup$

Using pandas, I would like to sum all of the price to get the count value for each one. The Dataframe looks like this:

   price  quantity
0     25         2
1     20         3
2     20        -1

My code:

import pandas as pd

df = pd.DataFrame({'price': [25, 20, 20], 'quantity': [2, 3, -1]})
print(df)

So, I would expect the output to be something like $(25*2)+(20*3)+(20*-1)$:

>>> 90
$\endgroup$
1
$\begingroup$

You could always call on the pandas DataFrame's columns and work with that.

values = df['price'] * df['quantity']
sum(values)

if you want more information, I recommend https://stackoverflow.com/questions/14059094/i-want-to-multiply-two-columns-in-a-pandas-dataframe-and-add-the-result-into-a-n

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.