# get max per row
max_val = df.max(axis=1)
# identify max and values after
m1 = df.eq(max_val, axis=0)
m2 = ~m1.cummax(axis=1)
# mask values after and get min per row
min_val = df.where(m1 | m2).min(axis=1)
# update DataFrame
df['Max'] = max_val
df['MinBefore'] = min_val
输出:
ab bc cd ef eh ij Max MinBefore
0 1 2 12 4 6 7 12 1.0
1 4 5 6 1 10 4 10 1.0
2 5 6 9 1 2 4 9 5.0
3 12 1 3 1 2 4 12 12.0
1条答案
按热度按时间lqfhib0f1#
您可以使用布尔掩码来查找最大值和先前值的位置:
输出: