我的输入火花数据帧是
Year Month Client Value
2019 1 1 5
2019 2 1 3
2019 3 1 4
2019 4 1 0
2019 5 1 3
2019 6 1 15
2019 7 1 45
2019 8 1 50
2019 9 1 64
2019 10 1 91
2019 11 1 1
2019 12 1 34
2019 1 2 0
2019 2 2 0
2019 3 2 0
2019 4 2 0
2019 5 2 1
2019 6 2 2
2019 7 2 10
2019 8 2 1
2019 9 2 4
2019 10 2 7
2019 11 2 1
2019 12 2 34
Dataframe按客户、年份和月份进行订购
如果过去6个月(2019-06年至2019-12年)的最大值大于90,“目标”列为3
如果过去6个月(2019-06年至2019-12年)的最大值大于15且小于90,“目标”列为2
如果过去6个月(2019-06年至2019-12年)的最大值小于15,则“目标”列为1
我根据上述数据共享了所需的输出
Year Month Client Value Target
2019 1 1 5 3
2019 2 1 3 3
2019 3 1 4 3
2019 4 1 0 3
2019 5 1 3 3
2019 6 1 15 3
2019 7 1 45 3
2019 8 1 50 3
2019 9 1 64 3
2019 10 1 91 3
2019 11 1 1 3
2019 12 1 34 3
2019 1 2 0 2
2019 2 2 0 2
2019 3 2 0 2
2019 4 2 0 2
2019 5 2 1 2
2019 6 2 2 2
2019 7 2 10 2
2019 8 2 1 2
2019 9 2 4 2
2019 10 2 7 2
2019 11 2 1 2
2019 12 2 34 2
你能帮我做这个吗
您可以筛选出6月份之前的所有月份,并为每个客户获得最大价值:
输出:
您可以使用
row_number
筛选相关行,获得最大值,从而获得目标,并将其连接回原始数据帧相关问题 更多 >
编程相关推荐