Value direction change in a pandas column after groupby

心不动则不痛 提交于 2019-12-13 04:38:54

问题


I have a dataframe as below.

Cycle	Type	Difference
2	2	0.001
2	2	-0.019
2	2	-0.023
2	2	-0.012
2	2	0.008
2	2	-0.003
2	2	0.005
2	2	-0.007
2	2	0.01
2	2	-0.008
2	2	-0.012
2	2	-0.015
2	2	-0.011
2	2	-0.021
3	2	-0.006
3	2	-0.026
3	2	-0.012
3	2	-0.011
3	2	0.001
3	2	-0.007
3	2	-0.005
3	2	0.002
3	2	-0.015
3	2	-0.015
3	2	-0.013
3	2	-0.009
3	2	-0.018
3	2	-0.015
3	2	-0.017
3	2	-0.004
3	2	-0.014

The values can be a few continuous negative & a few continuous positives. I want to add a column which has the sum of continuous positives & continuous negatives (column="Switch" in the result example), the a column which shows the number of continous positive of negative values(column="Switch count" All these have to be done after grouping the dafarame with 'Cycle' & 'Type'

The expected result is as below.

Cycle	Type	Difference	Switch	Switch count
2	2	0.001	0.01	1
		-0.019		
		-0.023		
		-0.012	-0.054	3
		0.008	0.008	1
		-0.003	-0.003	1
		0.005	0.005	1
		-0.007	-0.007	1
		0.01	0.01	1
		-0.008		
		-0.012		
		-0.015		
		-0.011		
		-0.021	-0.067	5
3	1	-0.006		
		-0.026		
		-0.012		
		-0.011	-0.055	4
		0.001	0.001	
		-0.007		
		-0.005	-0.012	2
		0.002	0.002	
		-0.015		
		-0.015		
		-0.013		
		-0.009		
		-0.018		
		-0.015		
		-0.017		
		-0.004		
		-0.014	-0.12	9

来源:https://stackoverflow.com/questions/56786932/value-direction-change-in-a-pandas-column-after-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!