I'd like to return the rows which has all columns > 0 or where only 2012 can be < 0.
import pandas as pd
import numpy as np
df = pd.DataFrame( {
'A': ['d','d','d','f','f','f','g','g','g','h','h','h'],
'B': [5,5,6,7,5,6,6,7,7,6,7,7],
'C': [1,1,1,1,1,1,1,1,1,1,1,1],
'S': [2012,2013,2014,2015,2016,2012,2013,2014,2015,2016,2012,2013]
} );
df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
print (df)
@jezrael, not exactly. I changed the dataframe to explain better. In the final result I need the rows where all columns are > 0 AND the ones where the columns are > 0, except for 2012. That one can be < 0. The result must show a new df with the columns that qualify. So, in the example below, g yes, d no.
df = pd.DataFrame( {
'A': ['d','d','d','d','d','d','g','g','g','g','g','g'],
'B': [5,5,6,-7,5,6,-6,7,7,6,-7,7],
'C': [1,1,1,1,1,1,1,1,1,1,1,1],
'S': [2012,2013,2014,2015,2016,2012,2012,2014,2015,2016,2012,2013]
} );
df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
S 2012 2013 2014 2015 2016
A
d 13 6 7 -6 6
g -11 8 8 8 7
EDITED Dataframe;
df = pd.DataFrame( {
'A': ['d','d','d','d','d','d','g','g','g','g','g','g',
'k','k','k','k','k','k'],
'B': [5,5,6,7,5,6,-6,7,7,6,-7,7,-8,7,-6,6,-7,50],
'C': [1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2],
'S': [2012,2013,2014,2015,2016,2012,2012,2014,2015,2016,2012,
2013,2012,2013,2014,2015,2016,2014]
} );
df = (df.B + df.C).groupby([df.A, df.S]).sum().unstack(fill_value=0)
print (df)
S 2012 2013 2014 2015 2016
A
d 13 6 7 8 6
g -11 8 8 8 7
k -6 9 48 8 -5