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) S 2012 2013 2014 2015 2016 A d 6 6 7 0 0 f 7 0 0 8 6 g 0 7 8 8 0 h 8 8 0 0 7
I want to add to the number of values ββthat were summed in the dataframe per year, as well as two additional columns [total years] and [total]
EDIT;
Dataframe should look something like this; S 2012 2012 2013 2013 2014 2014 2015 2015 Tot(sum) Tot(
EDIT 2;
@Jezrael, if I want to select only those rows that I need (as discussed in another question), I ran into problems with the same column names. How can we solve this?
EDIT 3;
btw, is it possible to use a generic link for column 2012, so i don't need to change the code in the future? something like the first column of a data frame; df_without_first column = df.drop (first column, axis = 1)