使用Pandas重命名groupby和count结果的列名

gg58donl  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(332)

给定以下 Dataframe :

import numpy as np
df = pd.DataFrame({'price': np.random.random_integers(0, high=100, size=100)})
ranges = [0,10,20,30,40,50,60,70,80,90,100]
df.groupby(pd.cut(df.price, ranges)).count()

输出:

price
 price  
(0, 10]     9
(10, 20]    11
(20, 30]    11
(30, 40]    9
(40, 50]    16
(50, 60]    7
(60, 70]    10
(70, 80]    9
(80, 90]    14
(90, 100]   4

我怎样才能reset_index结果并将列名重命名为binscounts呢?谢谢。

bins    counts
0   (0, 10]     9
1   (10, 20]    11
2   (20, 30]    11
3   (30, 40]    9
4   (40, 50]    16
5   (50, 60]    7
6   (60, 70]    10
7   (70, 80]    9
8   (80, 90]    14
9   (90, 100]   4
3npbholx

3npbholx1#

此代码可以工作但不够简洁,如果您有其他选择,欢迎分享:

df.groupby(pd.cut(df.price, ranges)).count()\
.rename(columns={'price' : 'counts'})\
.reset_index()\
.rename(columns={'price': 'bins'})

输出:

bins    counts
0   (0, 10]     9
1   (10, 20]    11
2   (20, 30]    11
3   (30, 40]    9
4   (40, 50]    16
5   (50, 60]    7
6   (60, 70]    10
7   (70, 80]    9
8   (80, 90]    14
9   (90, 100]   4
ycl3bljg

ycl3bljg2#

一种方法是将rename用于来自pd.cut的系列,因此,如果选择列price用于处理组,则输出为Series,因此添加Series.reset_index2 columns DataFramename参数:

df1 = (df.groupby(pd.cut(df.price, ranges).rename('bins'))['price'].count()
         .reset_index(name='counts'))
print (df1)
        bins  counts
0    (0, 10]      13
1   (10, 20]      13
2   (20, 30]       9
3   (30, 40]       9
4   (40, 50]       7
5   (50, 60]       9
6   (60, 70]       9
7   (70, 80]      12
8   (80, 90]       9
9  (90, 100]       9
hivapdat

hivapdat3#

df.groupby('team', as_index=False).agg(my_sum=('points', sum),my_max=('points', max))

https://www.statology.org/pandas-groupby-rename-column/

相关问题