Python:从重复行中取出特定行
Python: take specific rows from duplicated ones
我在尝试解决以下问题时迷失了方向:对于同样重复的每个日期,我想保存具有最高值的行(来自这组重复日期)。
例如这个:
date
Value
09/29
10
09/29
15
09/29
12
09/30
5
10/01
25
10/02
20
应该是这样的:
date
Value
09/29
15
09/30
5
10/01
25
我尝试使用 for 循环但没有任何结果。
df = df.groupby(by='date').max().reset_index()
print(df)
# before
date Value
0 09/29 10
1 09/29 15
2 09/29 12
3 09/30 5
4 10/01 25
5 10/01 20
# after
date Value
0 09/29 15
1 09/30 5
2 10/01 25
我在尝试解决以下问题时迷失了方向:对于同样重复的每个日期,我想保存具有最高值的行(来自这组重复日期)。
例如这个:
date | Value |
---|---|
09/29 | 10 |
09/29 | 15 |
09/29 | 12 |
09/30 | 5 |
10/01 | 25 |
10/02 | 20 |
应该是这样的:
date | Value |
---|---|
09/29 | 15 |
09/30 | 5 |
10/01 | 25 |
我尝试使用 for 循环但没有任何结果。
df = df.groupby(by='date').max().reset_index()
print(df)
# before
date Value
0 09/29 10
1 09/29 15
2 09/29 12
3 09/30 5
4 10/01 25
5 10/01 20
# after
date Value
0 09/29 15
1 09/30 5
2 10/01 25