gpt4 book ai didi

python - 在python中计算每年前10名的平均值(groupby,nlargest)

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:06 24 4
gpt4 key购买 nike

我想获取数据集中每年前 10 个“净利润”的平均值,并将其存储在新的 df 中。这是我目前所拥有的。

#loading the csv file
df = pd.read_csv('tmdb-movies.csv')

#Narrows down data to only include the last 25 years
df = df[df['release_year'] > 1992]

#Create column for calculated profit (revenue - budget)
df['Net Profit'] = (df['revenue_adj'] - df['budget_adj']) / 1000000

df = df['Net Profit'].groupby(df['release_year']).nlargest(10)

df.head(20)

这是我目前的输出:

release_year       
1993 10223 1293.766704
10225 628.371507
10226 490.390201
10222 451.732687
10312 345.086055
10260 344.534054
10240 312.158213
10234 279.252261
10228 272.728678
10250 226.825117
1994 4180 1093.391570
4179 916.422179
4187 483.382849
4181 471.414971
4203 434.906488
4186 388.200466
4197 365.866669
4182 338.760765
4177 302.944183
4194 246.901592
Name: Net Profit, dtype: float64

我想要的输出是这样的:

release_year      Average Net Profit
1993 400
1994 459
1995 480

最佳答案

level=0 分组,以便索引和使用 .mean()

df.groupby(df.index.get_level_values(level=0)).mean()

release_year
1993 464.484548
1994 504.219173
1995 511.392629
1996 424.185431
1997 543.448686
Name: Net Profit, dtype: float64

关于python - 在python中计算每年前10名的平均值(groupby,nlargest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52349597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com