1
df1 = pd.DataFrame({'Region': ['E', 'E', 'U', 'E'], 'Id': [1,None,None,None], 'Ids': [1,2,3,4]})
df2 = pd.DataFrame({'Region': ['E', 'U', 'U', 'E'], 'Id': [1,2,3,4], 'Ids': [1,2,3,4]})
x = df1.groupby(['Region']).count()
y = df2.groupby(['Region']).count()


c = pd.concat([x['Id'], y['Id']], axis=1, keys=['Here', 'There'])

I have the table with two rows (like indices, 'E' and 'U') which count the number of E and U for each data frame and concatenate them with different keys: Here and There. Now I want to add another index, let's call it 'Total' and next to it I want get the total number of values under 'Here' and 'There'.

Now:

Region Here There
E 1 2
U 0 2

Now I want to add another index, let's call it 'Total' and next to it I want get the total number of values under 'Here' and 'There'.

I want to achieve:

Region Here There
E 1 2
U 0 2
Total 1 4

Thank you.

0

2 Answers 2

1

You can add it simply by defining it as the sum:

c.loc['Total'] = c.sum()

Output:

        Here  There
Region             
E        1.0    2.0
U        0.0    2.0
Total    1.0    4.0
Sign up to request clarification or add additional context in comments.

1 Comment

@Jezrael Thanks for the quick edit :) - Rustam feel free to accept the answer if this solved the issue. Thanks
1
c.loc['total'] = c.sum(axis=0)

Output:

    Here    There
Region      
E   1   2
U   0   2
total   1   4

1 Comment

@RustamKarimov if it's good mark it as accepted

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.