【问题标题】:How to concat and transpose tow tables in python如何在python中连接和转置两个表
【发布时间】:2020-06-29 00:19:36
【问题描述】:

我不知道为什么我的代码不起作用,我想在 python 中转置和连接拖车表。 我的代码:

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])

输出应该是:

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

【问题讨论】:

    标签: python pandas concat


    【解决方案1】:

    append 在分配名称之后

    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
    

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2021-10-04
      • 2023-04-09
      • 2021-08-06
      • 2013-11-02
      • 2020-09-11
      相关资源
      最近更新 更多