gpt4 book ai didi

python - 根据多列和条件对数据框进行排序

转载 作者:行者123 更新时间:2023-12-01 00:34:42 24 4
gpt4 key购买 nike

我尝试首先根据 rolls 降序对以下数据帧进行排序,然后按 diff_vto 升序(对于正值),最后按 diff_vto 升序对于负值。这是原始数据框:

    day  prob  vto  rolls  diff  diff_vto
0 1 10 14 27.0 0.0 -13
1 2 10 14 20.0 3.0 -12
2 3 7 14 16.0 4.0 -11
3 4 3 14 12.0 -3.0 -10
4 5 6 14 17.0 3.0 -9
5 6 3 14 14.0 -5.0 -8
6 7 8 14 14.0 5.0 -7
7 8 3 14 9.0 0.0 -6
8 9 3 14 9.0 0.0 -5
9 10 3 14 17.0 0.0 -4
10 11 3 14 22.0 -8.0 -3
11 12 11 14 27.0 3.0 -2
12 13 8 14 23.0 0.0 -1
13 14 8 14 25.0 1.0 0
14 15 7 14 27.0 -3.0 1

这是代码,以防您希望复制它:

    import pandas as pd 
a = {'day':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],'prob':[10,10,7,3,6,3,8,3,3,3,3,11,8,8,7],'vto':[14,14,14,14,14,14,14,14,14,14,14,14,14,14,14]}
df = pd.DataFrame(a)
df.loc[len(df)+1] = df.loc[0] #Add an extra 2 days for rolling rolling
df.loc[len(df)+2] = df.loc[1] #Add an extra 2 days for rolling
df['rolls'] = df['prob'].rolling(3).sum()
df['rolls'] = df['rolls'].shift(periods=-2) #Displace rolls to match the index + 2
df['diff'] = df['prob'].diff(periods=-1) #Prob[i] - Prob[i+1]
df['diff_vto'] = df['day'] - df['vto']
df = df.head(15)
print(df)

我希望能够根据 rolls (降序)对数据帧进行排序,后跟 diff_vto 的最小值(当它为正数时)(升序),然后是diff_vto 为负数(升序)时的最小值。根据上面发布的数据框,这将是预期的输出:

    day  prob  vto  rolls  diff  diff_vto
14 15 7 14 27.0 -3.0 1
0 1 10 14 27.0 0.0 -13
11 12 11 14 27.0 3.0 -2
13 14 8 14 25.0 1.0 0
12 13 8 14 23.0 0.0 -1
10 11 3 14 22.0 -8.0 -3
1 2 10 14 20.0 3.0 -12
4 5 6 14 17.0 3.0 -9
9 10 3 14 17.0 0.0 -4
2 3 7 14 16.0 4.0 -11
5 6 3 14 14.0 -5.0 -8
6 7 8 14 14.0 5.0 -7
3 4 3 14 12.0 -3.0 -10
7 8 3 14 9.0 0.0 -6
8 9 3 14 9.0 0.0 -5

我显然已经尝试应用 .sort_values() 但我无法让条件排序在 diff_vto 上工作,因为将其设置为升序显然会放置负值在积极的事情之前。我可以得到建议吗?谢谢。

最佳答案

您希望按 diff_vto>0abs(diff_vto) 排序,两者均递减:

df['pos'] = df['diff_vto'].gt(0)
df['abs'] = df['diff_vto'].abs()

df.sort_values(['rolls', 'pos', 'abs'], ascending=[False, False, False])

输出(如果需要,您可以删除 posabs):

    day  prob  vto  rolls  diff  diff_vto    pos  abs
14 15 7 14 27.0 -3.0 1 True 1
0 1 10 14 27.0 0.0 -13 False 13
11 12 11 14 27.0 3.0 -2 False 2
13 14 8 14 25.0 1.0 0 False 0
12 13 8 14 23.0 0.0 -1 False 1
10 11 3 14 22.0 -8.0 -3 False 3
1 2 10 14 20.0 3.0 -12 False 12
4 5 6 14 17.0 3.0 -9 False 9
9 10 3 14 17.0 0.0 -4 False 4
2 3 7 14 16.0 4.0 -11 False 11
5 6 3 14 14.0 -5.0 -8 False 8
6 7 8 14 14.0 5.0 -7 False 7
3 4 3 14 12.0 -3.0 -10 False 10
7 8 3 14 9.0 0.0 -6 False 6
8 9 3 14 9.0 0.0 -5 False 5

关于python - 根据多列和条件对数据框进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57891587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com