How to concat and transpose tow tables in python

Viewed 38

I do not know why my code is not working, I want to transpose and concat tow tables in python. my code:

import numpy as np
import pandas as pd
np.random.seed(100)

df = pd.DataFrame({'TR':np.arange(1, 6).repeat(5), 'A': np.random.randint(1, 100,25), 'B':  np.random.randint(50, 100,25), 'C':  np.random.randint(50, 1000,25), 'D':  np.random.randint(5, 100,25) })
table = df.groupby('TR').mean().round(decimals=1)
table2 = df.drop(['TR'], axis=1).sem().round(decimals=1)
table2 = table2.T
pd.concat([table, table2])

The output should be:

TR   A     B      C     D
                         
1   54.0  68.6  795.8  49.8  
2   61.4  67.8  524.8  52.8  
3   54.0  73.6  556.6  46.6  
4   35.6  69.2  207.2  46.4  
5   44.4  85.0  639.8  73.8    
st 6.5 3.4   62.5  6.4  

      

output

1 Answers

append after assign name

table2.name='st'
table=table.append(table2)
table
       A     B      C     D
TR                         
1   55.8  73.2  536.8  42.8
2   31.0  75.4  731.2  43.6
3   42.0  68.8  598.6  32.4
4   33.6  79.0  300.8  43.6
5   70.2  72.2  566.8  54.8
st   5.9   3.2   62.5   5.9
Related