Pandas dataframe containining a few columns. Let's say years, production, num of units, price per unit.
I know how to get total of production for all the existing years in the DF using the sum() function.
Now I want to find the production for a specific year, let's say 2014. Now the years repeat in the dataframe. How do I write code to find the sum of all the values of production in the dataframe when year = 2014?
Do I use the groupby function? But how do I do that effectively to achieve this result?
Thank you!
Using arbitrary values for the variables that you listed,
import pandas as pd
years = list(range(2010, 2020))
years.extend([2014, 2014])
production = list(range(12))
num = list(range(12))
price = list(range(12))
df = pd.DataFrame({'years':years, 'prod': production, 'num': num,\
'price': price})
answer = df[df['years'] == 2014]['prod'].sum().item()
print(answer)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.