0

Given the following dataframe:

branch  Year    Month   count
BILING  2017    8       1871
BILING  2018    8       1208
BILING  2019    8       4
BILWPL  2017    8       109
BILWPL  2018    8       131
BILWPL  2019    8       84
BRANCHB 2017    8       1871
BRANCHB 2018    8       1208
BRANCHB 2019    8       4

The resulting dataframe needs to look like this:

branch  Year    Month   count
BILING  2017    8       1980
BILING  2018    8       1339
BILING  2019    8       88
BRANCHB 2017    8       1871
BRANCHB 2018    8       1208
BRANCHB 2019    8       4

Where I'm at:

biling = circ.loc[circ['branch'] == 'BILING']
biwpl = circ.loc[circ['branch'] == 'BIWPL']

biling = biling + biwpl

If I try to print biling:

print (biling)

Out:

branch  Year  Month  count
0    NaN   NaN    NaN    NaN
1    NaN   NaN    NaN    NaN
2    NaN   NaN    NaN    NaN

UPDATE Answered by @rafaelc. See comments. ( Thanks! )

Bubnoff
  • 3,917
  • 3
  • 30
  • 33
  • `df.groupby('Year').agg({'branch': 'first', 'Month': 'first', 'count': 'sum'})` – rafaelc Sep 13 '19 at 21:00
  • I copied the df and tried this with no success: `circ2.groupby('Year').agg({'branch': ['BILING','BILWPL'], 'count': 'sum'})` – Bubnoff Sep 13 '19 at 21:26
  • 1
    Ah I see. The this is even simpler. First, rename all your branches the way you want. For example, `df.loc[df.branch.isin(['BILING', 'BILWPL']), 'branch'] = 'BILING'` . Then this is a simple `groupby` and `sum` – rafaelc Sep 13 '19 at 21:30
  • 1
    @Bubnoff that's just the name of your column.. replace for the actual name.. `df.column_name_here.isin......` – rafaelc Sep 13 '19 at 21:37
  • `circ2.loc[circ2.branch.isin(['BILING', 'BILWPL']), 'branch'] = 'BILING'` `circ2.groupby([circ2.branch, circ2.Year])['count'].sum()` -- I had typoed the df in the last comment, sorry for the confusion! This seems to do the trick! – Bubnoff Sep 13 '19 at 21:39
  • 1
    There you go! Glad I could help ;] – rafaelc Sep 13 '19 at 21:40

0 Answers0