如何使用 python 到 select table 中每组具有最小值的所有行
How to use python to select all rows with minimum values for each group in a table
X
Y
Val
GrpID
414995.44
5813554.46
2.848
1
414995.25
5813554.17
2.847
1
414995.40
5813554.39
2.840
1
414995.35
5813554.32
2.840
1
414995.31
5813554.25
2.840
1
414995.21
5813554.11
2.856
1
414995.15
5813554.03
2.876
1
414995.13
5813554.96
2.888
1
414976.71
5813526.54
0.768
2
414976.62
5813526.39
0.744
2
414976.37
5813526.03
0.712
2
414976.33
5813525.96
0.712
2
414976.23
5813525.81
0.728
2
414976.13
5813525.67
0.736
2
414976.08
5813525.59
0.745
2
我想要的结果如下图:
X
Y
Val
GrpID
414995.40
5813554.39
2.840
1
414995.35
5813554.32
2.840
1
414995.31
5813554.25
2.840
1
414976.37
5813526.03
0.712
2
414976.33
5813525.96
0.712
2
请尝试使用 groupby 的 return 作为切片的参数:
df[df['Val'].isin(df.groupby('GrpID')['Val'].min())]
Returns:
X Y Val GrpID
2 414995.40 5813554.39 2.840 1
3 414995.35 5813554.32 2.840 1
4 414995.31 5813554.25 2.840 1
10 414976.37 5813526.03 0.712 2
11 414976.33 5813525.96 0.712 2
X | Y | Val | GrpID |
---|---|---|---|
414995.44 | 5813554.46 | 2.848 | 1 |
414995.25 | 5813554.17 | 2.847 | 1 |
414995.40 | 5813554.39 | 2.840 | 1 |
414995.35 | 5813554.32 | 2.840 | 1 |
414995.31 | 5813554.25 | 2.840 | 1 |
414995.21 | 5813554.11 | 2.856 | 1 |
414995.15 | 5813554.03 | 2.876 | 1 |
414995.13 | 5813554.96 | 2.888 | 1 |
414976.71 | 5813526.54 | 0.768 | 2 |
414976.62 | 5813526.39 | 0.744 | 2 |
414976.37 | 5813526.03 | 0.712 | 2 |
414976.33 | 5813525.96 | 0.712 | 2 |
414976.23 | 5813525.81 | 0.728 | 2 |
414976.13 | 5813525.67 | 0.736 | 2 |
414976.08 | 5813525.59 | 0.745 | 2 |
我想要的结果如下图:
X | Y | Val | GrpID |
---|---|---|---|
414995.40 | 5813554.39 | 2.840 | 1 |
414995.35 | 5813554.32 | 2.840 | 1 |
414995.31 | 5813554.25 | 2.840 | 1 |
414976.37 | 5813526.03 | 0.712 | 2 |
414976.33 | 5813525.96 | 0.712 | 2 |
请尝试使用 groupby 的 return 作为切片的参数:
df[df['Val'].isin(df.groupby('GrpID')['Val'].min())]
Returns:
X Y Val GrpID
2 414995.40 5813554.39 2.840 1
3 414995.35 5813554.32 2.840 1
4 414995.31 5813554.25 2.840 1
10 414976.37 5813526.03 0.712 2
11 414976.33 5813525.96 0.712 2