简体   繁体   English

当groupby另一个时,pandas在组中最少获得一列

[英]pandas get minimum of one column in group when groupby another

I have a pandas dataframe that looks like this:我有一个如下所示的 Pandas 数据框:

      c     y
0     9     0
1     8     0
2     3     1
3     6     2
4     1     3
5     2     3
6     5     3
7     4     4
8     0     4
9     7     4

I'd like to groupby y and get the min and max of c so that my new dataframe would look like this:我想分组y并获取c的最小值和最大值,以便我的新数据框如下所示:

      c     y     min   max
0     9     0     8     9
1     8     0     8     9
2     3     1     3     3   
3     6     2     6     6 
4     1     3     1     5
5     2     3     1     5
6     5     3     1     5
7     4     4     0     7
8     0     4     0     7
9     7     4     0     7

I tried using df['min'] = df.groupby(['y'])['c'].min() but that gave me some weird results.我尝试使用df['min'] = df.groupby(['y'])['c'].min()但这给了我一些奇怪的结果。 The first 175 rows were populated in the min column but then it went to NaN for all the rest.前 175 行填充在 min 列中,但随后所有其他行都变为 NaN。 is that not how you're supposed to use the groupby method?这不是你应该如何使用 groupby 方法吗?

Option 1 Use transform选项 1使用transform

In [13]: dfc = df.groupby('y')['c']

In [14]: df.assign(min=dfc.transform(min), max=dfc.transform(max))
Out[14]:
   c  y  max  min
0  9  0    9    8
1  8  0    9    8
2  3  1    3    3
3  6  2    6    6
4  1  3    5    1
5  2  3    5    1
6  5  3    5    1
7  4  4    7    0
8  0  4    7    0
9  7  4    7    0

Or或者

In [15]: df['min' ] = dfc.transform('min')

In [16]: df['max' ] = dfc.transform('max')

Option 2 Use join and agg选项 2使用 join 和 agg

In [30]: df.join(df.groupby('y')['c'].agg(['min', 'max']), on='y')
Out[30]:
   c  y  min  max
0  9  0    8    9
1  8  0    8    9
2  3  1    3    3
3  6  2    6    6
4  1  3    1    5
5  2  3    1    5
6  5  3    1    5
7  4  4    0    7
8  0  4    0    7
9  7  4    0    7

Option 3 Use merge and agg选项 3使用合并和聚合

In [28]: df.merge(df.groupby('y')['c'].agg(['min', 'max']), right_index=True, left_on='y')
Out[28]:
   c  y  min  max
0  9  0    8    9
1  8  0    8    9
2  3  1    3    3
3  6  2    6    6
4  1  3    1    5
5  2  3    1    5
6  5  3    1    5
7  4  4    0    7
8  0  4    0    7
9  7  4    0    7

With Numpy shenanigans使用 Numpy 恶作剧

n = df.y.max() + 1
omax = np.ones(n, df.c.values.dtype) * df.c.values.min()
omin = np.ones(n, df.c.values.dtype) * df.c.values.max()
np.maximum.at(omax, df.y.values, df.c.values)
np.minimum.at(omin, df.y.values, df.c.values)

df.assign(min=omin[df.y], max=omax[df.y])

   c  y  min  max
0  9  0    8    9
1  8  0    8    9
2  3  1    3    3
3  6  2    6    6
4  1  3    1    5
5  2  3    1    5
6  5  3    1    5
7  4  4    0    7
8  0  4    0    7
9  7  4    0    7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas groupby 获取另一列最小的列的值 - Pandas groupby get value of a column where another column is minimum 熊猫按一列分组,然后按另一列分组 - pandas groupby one column and then groupby another column 熊猫如何对一个列进行分组并根据另一列的最小唯一值过滤数据框? - How to pandas groupby one column and filter dataframe based on the minimum unique values of another column? 如何在一列中执行 groupby 并计算 pandas 中每一组中另一列的不同值 - How to perform groupby in one column and count distinct values of another column in each group in pandas Pandas groupby()在一列上,然后在另一列上求和 - Pandas groupby() on one column and then sum on another Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 使用 Pandas,根据第二列的最小值从数据框中的一列(对于每组)获取值 - With Pandas, get value from one column in dataframe (for each group), based on minimum value of second column Pandas Groupby:根据另一列的值从组的前一个元素中获取值 - Pandas Groupby: get value from previous element of a group based on value of another column pandas groupby,其中您获得一列的最大值和另一列的最小值 - pandas groupby where you get the max of one column and the min of another column 如何按一列分组并在另一列中获取所有常见值 pandas - How to groupby one column and get all common values in another column pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM