python - 如何在python中连接和转置两个表

标签 python pandas concat

我不知道为什么我的代码不起作用,我想在 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

最佳答案

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

关于python - 如何在python中连接和转置两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62629578/

相关文章:

python - Pandas :将行值设置为与索引号对应的字母表中的字母?

python - 仅转换每列的第一个和最后一个填充值之间的 nan

mysql - 从 MySQL JSON 数组中获取不同的值

r - 将数据帧组合在两个不同的列表中,这些列表以 R 中的元素名称为键

python - scrapy重定向到127.0.0.1

python - 我可以使用 mod_python 在 Apache 服务器上运行 Flask 吗?

python - Django ORM : get objects distinct on a column and ordered by another column

python - 打印错误类型、错误语句和自身语句

python - 截断 pandas 数据框中的行数

python - 如何在python中使用for循环垂直连接1000张图像?