【问题标题】:Python Pandas Panel counting value occurencePython Pandas Panel 计数值出现
【发布时间】:2015-02-18 10:21:36
【问题描述】:

我有一个存储为熊猫面板的大型数据集。我想计算面板中每个项目在小轴上出现的值

    #%% Creating the first Dataframe
    dates1 = pd.date_range('2014-10-19','2014-10-20',freq='H')
    df1 = pd.DataFrame(index = dates)
    n1 = len(dates)

    df1.loc[:,'a'] = np.random.uniform(3,10,n1)
    df1.loc[:,'b'] = np.random.uniform(0.9,1.2,n1)

    #%% Creating the second DataFrame
    dates2 = pd.date_range('2014-10-18','2014-10-20',freq='H')
    df2 = pd.DataFrame(index = dates2)
    n2 = len(dates2)

    df2.loc[:,'a'] = np.random.uniform(3,10,n2)
    df2.loc[:,'b'] = np.random.uniform(0.9,1.2,n2)

    #%% Creating the panel from both DataFrames
    dictionary = {}
    dictionary['First_dataset'] = df1
    dictionary['Second dataset'] = df2

    P = pd.Panel.from_dict(dictionary)

    #%% I want to count the number of values < 1.0 for all datasets in the panel
    ## Only for minor axis b, not minor axis a, stored seperately for each dataset
    for dataset in P:
        P.loc[dataset,:,'b'] #I need to count the numver of values <1.0 in this pandas_series

【问题讨论】:

  • 在我尝试这个之前从未处理过面板:P[P.minor_axis == 'a'].min() 它做你想要的吗?

标签: python pandas panel


【解决方案1】:

要计算所有小于 1.0 的“b”值,我首先通过交换短轴和项目将 b 隔离在它自己的 DataFrame 中。

In [43]: b = P.swapaxes("minor","items").b

In [44]: b.where(b<1.0).stack().count()
Out[44]: 30

【讨论】:

    【解决方案2】:

    感谢您与我一起思考,但经过数小时的尝试,我设法找到了一个非常简单的解决方案。我想我应该分享它以防其他人正在寻找类似的解决方案。

        for dataset in P:
            abc = P.loc[dataset,:,'b']
            abc_low = sum(i < 1.0 for i in abc)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-08
      • 2013-06-30
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      相关资源
      最近更新 更多