编写一个 Python 程序来查找面板中第一行的平均值
假设您有Panel并且第一行的平均值是,
Averageoffirstrowis: Column1 0.274124 dtype: float64
解决方案
为了解决这个问题,我们将按照下面给出的步骤-
将数据值设置为字典键为'Column1',值为pd.DataFrame(np.random.randn(5,3))
data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}将数据分配给Panel并将其保存为p
p = pd.Panel(data)
使用字典键Column1打印列
print(p['Column1'])
使用major_xs(0)计算第一行的平均值,
p.major_xs(0).mean()
例子
让我们看下面的代码以获得更好的理解-
import pandas as pd
import numpy as np
data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}
p = pd.Panel(data)
print("Panelvalues:")
print(p['Column1'])
print("Averageoffirstrowis:")
print(p.major_xs(0).mean())输出
Panelvalues:
0 1 2
0 0.629910 0.275741 -0.083281
1 -0.509143 -1.794204 0.300390
2 -1.944141 0.085508 -0.155167
3 1.551087 -0.671242 -0.838922
4 -0.643543 0.622777 1.112745
Averageoffirstrowis:
Column1 0.274124
dtype: float64热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短