我想获取数据集中每年排名前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
答案 0 :(得分:2)
按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
答案 1 :(得分:2)
您可以通过mean
通过level
df.mean(level=0)