Pandas - 计算组内某列最小值的最大值。

3

我有一个大的数据框df. 我想从这个数据框中,在由['depth','period','direction']的唯一值定义的组内,取出具有最小“tension”值并且给出最大“height”值的行。因此,我想在指定的组内计算给出最大高度的最小张力量。

所以从以下数据框df:

depth,height,period,direction,tension,parameter1
8,2.75,4,180,5,16.7
8,3,4,180,10,21.6
8,3,4,180,15,26.6
8,3,4,180,20,31.6
15,2.5,7,45,5,22.8
15,2.5,7,45,10,27.8
15,2.75,7,45,15,32.7
15,3,7,45,20,37.8
22,2.5,7,67.5,5,28.9
22,2.75,7,67.5,10,33.9
22,2.75,7,67.5,15,38.9
22,2.75,7,67.5,20,43.8

我希望返回一个名为df1的新数据框:
depth,height,period,direction,tension,parameter1
8,3,4,180,10,21.6
15,3,7,45,20,37.8
22,2.75,7,67.5,10,33.9

你有任何想法如何做到这一点吗?

2个回答

3

按升序对tensionheight进行排序,分别按降序排列,并按组取第一行:

(
  df.sort_values('tension')
    .sort_values('height', ascending=False, kind='mergesort')
    .groupby(['depth', 'period', 'direction'])
    .head(1)
)
   depth  height  period  direction  tension  parameter1
1      8    3.00       4      180.0       10        21.6
7     15    3.00       7       45.0       20        37.8
9     22    2.75       7       67.5       10        33.9

2

尝试:

df = df.sort_values(
    by=["depth", "period", "direction", "height", "tension"],
    ascending=[True, True, True, True, False],
)
df = df.drop_duplicates(subset=["depth", "period", "direction"], keep="last")
print(df)

输出:

   depth  height  period  direction  tension  parameter1
1      8    3.00       4      180.0       10        21.6
7     15    3.00       7       45.0       20        37.8
9     22    2.75       7       67.5       10        33.9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接