数据帧的平均值经过转置后

问题描述 投票:0回答:1

我正在迭代多个文件,对于每个文件,我都会得到一个数据框。

enter image description here

然后我使用以下方法计算每个数据帧的平均值

BinA_Data.mean(axis=0,skipna=True, numeric_only=True))

但是我得到的结果是一系列,其中索引是列名称就像

enter image description here

但实际上我希望平均值与列名“按原样”显示

enter image description here

稍后我想连接为每个文件计算的平均值以显示类似这样的内容。

enter image description here

我尝试使用 transpose() 但平均值仍然始终以列名作为索引显示,并且我丢失了我想要保留的列名。有什么建议吗?

以上是样本数据的实际平均值与显示的平均值不匹配。

pandas dataframe mean transpose
1个回答
0
投票

示例代码

您不应以图像形式提供示例样本,而应提供最小且可重现的示例。由于您的代码(

BinA_Data.mean(axis=0,skipna=True, numeric_only=True)
)生成的结果是一个系列,因此我们将生成一个简单的系列
s
作为示例

import pandas as pd
s = pd.Series([1, 2, 3], index=['A', 'B', 'C'])

s

A    1
B    2
C    3
dtype: int64

代码

由于级数是一维的,因此不能转置。转换为数据框并尝试转置。

out = s.to_frame().T

    A   B   C
0   1   2   3
© www.soinside.com 2019 - 2024. All rights reserved.