【问题标题】:Calculate the mean from excel sheet for specific rows从excel表中计算特定行的平均值
【发布时间】:2021-05-16 22:27:42
【问题描述】:

大家好!我正在努力计算某些行的平均值 使用python的excel表。特别是,我想计算每三行的平均值,从前三行开始,然后移动到接下来的三行,依此类推。我的 Excel 表包含 156 行数据。 我的数据表如下所示:

And this is my code: 
import numpy 
import pandas as pd
df = pd.read_excel("My Excel.xlsx")
x = df.iloc[[0,1,2], [9,10,11]].mean()
print(x)

总而言之,我正在尝试计算第 1 部分测量值 1(第 1、2、3 行)的平均值和第 2 部分的平均值 使用一行代码或某种索引进行测量 1(第 9、10、11 行)。我希望收到两个数字列表,一个代表第 1 部分测量 1 的平均值(第 1、2、3 行),另一个代表第 2 部分测量 1 的平均值(第 10、11、12 行)。我也熟悉python将第一行计为0的事实。索引应该是n+1的形式。

提前谢谢你。

【问题讨论】:

    标签: python excel pandas indexing mean


    【解决方案1】:

    您可以(例如)为要计算的每个平均值生成一个列表:

    x1, x2 = list(df.iloc[[0,1,2]].mean()), list(df.iloc[[9,10,11]].mean())
    

    或者您也可以生成列表列表:

    x = [list(df.iloc[[0,1,2]].mean()), list(df.iloc[[9,10,11]].mean())]
    

    【讨论】:

    • 第二种方法看起来很不错。我已经尝试过了,但我得到的 'list' 对象没有属性 'mean'。
    • 我忘记了 "mean()" 后面的括号,现在已编辑。
    • 我注意到了,但还是一样
    • x1, x2 = list(df.iloc[[0,1,2]]).mean(), list(df.iloc[[9,10,11]]).mean( )。我已经安装了统计库。
    • 在你评论的内容中,mean() 后面仍然没有括号
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多