【问题标题】:Finding min, max, avg in Pandas, Python for all rows with the same first column在 Pandas、Python 中查找具有相同第一列的所有行的最小值、最大值、平均值
【发布时间】:2020-04-06 17:43:46
【问题描述】:

是否可以找到具有相同第一列的所有数据的最小值、最大值和平均值?

例如,对于第一列1_204192587

  1. 考虑从 4 到 n 的所有行和列

  2. 查找第 4+ 列中所有条目的最小值、最大值和平均值,以及第一列中具有 **1_204192587** 值的所有行。

    意思是,为下面显示的每个唯一 Start 值做一种描述数据。

 `In: data.groupby(["Start"]).groups.keys()

 out: dict_keys(['1_204192587', '1_204197200'])`

This is how data frame looks like

我试过了

df=data.groupby(["Start"]).describe() 

但这不是我想要的。

我也尝试在描述时指定轴,

data.apply.(pd.DataFrame.describe, axis=1) 

但我得到了错误。

期望的输出

unique key/first column value   MIN   MAX   AVG
 1_204192587                    *     *      *
 1_204197200                    *     *      *

我是初学者,提前感谢您的任何回复。

【问题讨论】:

标签: python pandas max average min


【解决方案1】:

您可以使用以下内容:

df.loc[4:].describe()

df 是您的数据框
[4:] 选择第 5 行,然后在
.describe() 上为您提供统计摘要(平均值、平均值...)

您还可以添加.transpose() 和结尾以获得您询问的输出。

如果你想将它分配给另一个变量(数据框)

所以它看起来像:

new_df = df.loc[4:].describe().trasnpose()

【讨论】:

    【解决方案2】:

    我认为您想比较每组的所有数字列,因此将 Start 列转换为 index,然后通过 DataFrame.select_dtypes 选择数字列,通过 DataFrame.stack 重塑并最后使用 DataFrameGroupBy.describe 按索引:

        data = pd.DataFrame({
            'A':list('abcdef'),
             'B':[4,5,4,5,5,4],
             'C':[7,8,9,4,2,3],
             'D':[1,3,5,7,1,0],
             'E':[5,3,6,9,2,4],
             'Start':list('aaabbb')
    })
    df1 = data.set_index("Start").select_dtypes(np.number).stack().groupby(level=0).describe() 
    print (df1)
           count      mean       std  min   25%  50%   75%  max
    Start                                                      
    a       12.0  5.000000  2.256304  1.0  3.75  5.0  6.25  9.0
    b       12.0  3.833333  2.516611  0.0  2.00  4.0  5.00  9.0
    

    或者通过GroupBy.agg指定聚合函数列表:

    df2 = (data.set_index("Start")
               .select_dtypes(np.number)
               .stack()
               .groupby(level=0)
               .agg(['min','max','mean']))
    print (df2)
           min  max      mean
    Start                    
    a        1    9  5.000000
    b        0    9  3.833333
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 2015-01-16
      • 1970-01-01
      • 2012-09-27
      • 2016-01-05
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      相关资源
      最近更新 更多