How to sum negative and positive values separately when using groupby in pandas?
问题 How to sum positive and negative values differently in pandas and put them let's say in positive and negative columns? I have this dataframe like below: df = pandas.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], 'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], 'C' : np.random.randn(8), 'D' : np.random.randn(8)}) Output is as below: df A B C D 0 foo one 0.374156 0.319699 1 bar one -0.356339 -0.629649 2 foo two -0.390243 -1.387909 3 bar three -0