Python 如何计算熊猫特定分界点前后的统计特性?

Python 如何计算熊猫特定分界点前后的统计特性?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个熊猫数据框,看起来像这样: import pandas as pd dt = pd.DataFrame({'idx':[1,2,3,4,5,1,2,3,4,5], 'id':[1,1,1,1,1,2,2,2,2,2], 'value':[5,10,15,20,25, 55,65,75,85,97]}) dt_idx = pd.DataFrame({'cutoff':[1,1,1,3,3,3,3,3,5,5,5,5,2,2,2,2,2,2,2,4,4]}) 我还有另一个类似的例子: i

我有一个熊猫数据框,看起来像这样:

import pandas as pd
dt = pd.DataFrame({'idx':[1,2,3,4,5,1,2,3,4,5], 'id':[1,1,1,1,1,2,2,2,2,2], 'value':[5,10,15,20,25, 55,65,75,85,97]})
dt_idx = pd.DataFrame({'cutoff':[1,1,1,3,3,3,3,3,5,5,5,5,2,2,2,2,2,2,2,4,4]})
我还有另一个类似的例子:

import pandas as pd
dt = pd.DataFrame({'idx':[1,2,3,4,5,1,2,3,4,5], 'id':[1,1,1,1,1,2,2,2,2,2], 'value':[5,10,15,20,25, 55,65,75,85,97]})
dt_idx = pd.DataFrame({'cutoff':[1,1,1,3,3,3,3,3,5,5,5,5,2,2,2,2,2,2,2,4,4]})
对于本玩具示例中dt_idx的3个最常见截止值,它是3,5和2,我想获得dt数据帧值列的平均值和std,用于以下两组:

idx截止
有一种类似蟒蛇的方法吗?

这里的简单循环是一个不错的选择。使用value_计数获取您关心的截止值,然后循环这些截止值。您可以使用groupby同时获取这两个。将所有内容存储在一个dict中,并按截止值设置关键帧,然后您可以通过concat获得一个具有多索引的数据帧

d = {}
for cutoff in dt_idx.cutoff.value_counts().head(3).index:
    d[cutoff] = dt.groupby(dt.idx.gt(cutoff))['value'].agg(['mean', 'std'])
    
pd.concat(d, names=['cutoff', 'greater_than_cutoff'])
如果您想使用这些截止值作为范围,那么我们将创建列表,在末尾添加np.inf,并且我们可以使用带有pd.cut的单个groupby来创建组

bins = dt_idx.cutoff.value_counts().head(3).index.sort_values().tolist() + [np.inf]
#[2, 3, 5, inf]

dt.groupby(pd.cut(dt.idx, bins, right=False))['value'].agg(['mean', 'std'])
#             mean        std
#idx                         
#[2.0, 3.0)  37.50  38.890873
#[3.0, 5.0)  48.75  36.371921
#[5.0, inf)  61.00  50.911688

这里的简单循环是一个很好的选择。使用value_计数获取您关心的截止值,然后循环这些截止值。您可以使用groupby同时获取这两个。将所有内容存储在一个dict中,并按截止值设置关键帧,然后您可以通过concat获得一个具有多索引的数据帧

d = {}
for cutoff in dt_idx.cutoff.value_counts().head(3).index:
    d[cutoff] = dt.groupby(dt.idx.gt(cutoff))['value'].agg(['mean', 'std'])
    
pd.concat(d, names=['cutoff', 'greater_than_cutoff'])
如果您想使用这些截止值作为范围,那么我们将创建列表,在末尾添加np.inf,并且我们可以使用带有pd.cut的单个groupby来创建组

bins = dt_idx.cutoff.value_counts().head(3).index.sort_values().tolist() + [np.inf]
#[2, 3, 5, inf]

dt.groupby(pd.cut(dt.idx, bins, right=False))['value'].agg(['mean', 'std'])
#             mean        std
#idx                         
#[2.0, 3.0)  37.50  38.890873
#[3.0, 5.0)  48.75  36.371921
#[5.0, inf)  61.00  50.911688

首先我们得到3个最常见的值,然后我们对每个值使用

import numpy as np
n=3
l = dt_idx['cutoff'].value_counts()[:n].index

new_df = pd.concat({val : dt.groupby(np.where(dt['idx'].le(val),
                                              'less than or equal',
                                              'higher'))['value']
                            .agg(['mean','std'])
                    for val in l}, axis=1)
print(new_df)

                            2                 3                5           
                         mean        std   mean        std  mean        std
higher              52.833333  36.771819  56.75  39.903007   NaN        NaN
less than or equal  33.750000  30.652624  37.50  30.943497  45.2  34.080949

#new_df.stack(0).swaplevel().sort_index()
#                           mean        std
#2 higher              52.833333  36.771819
#  less than or equal  33.750000  30.652624
#3 higher              56.750000  39.903007
#  less than or equal  37.500000  30.943497
#5 less than or equal  45.200000  34.080949

首先我们得到3个最常见的值,然后我们对每个值使用

import numpy as np
n=3
l = dt_idx['cutoff'].value_counts()[:n].index

new_df = pd.concat({val : dt.groupby(np.where(dt['idx'].le(val),
                                              'less than or equal',
                                              'higher'))['value']
                            .agg(['mean','std'])
                    for val in l}, axis=1)
print(new_df)

                            2                 3                5           
                         mean        std   mean        std  mean        std
higher              52.833333  36.771819  56.75  39.903007   NaN        NaN
less than or equal  33.750000  30.652624  37.50  30.943497  45.2  34.080949

#new_df.stack(0).swaplevel().sort_index()
#                           mean        std
#2 higher              52.833333  36.771819
#  less than or equal  33.750000  30.652624
#3 higher              56.750000  39.903007
#  less than or equal  37.500000  30.943497
#5 less than or equal  45.200000  34.080949

是否可以删除多索引并使用常规索引?这样信息就会丢失。。请记住,您有3个自由度均值或方差,更低或更高,以及级别2、3或5。这就是为什么您需要多索引,另一个选项是使用数据帧字典@quant@ALollz也许这应该是一个单独的问题,但是否有可能获得截止值组内的平均值和std?所以在这种情况下,它将是群[2,3,5,[5,inf@quant呃,这已经足够接近了。类似的方法来获取值,我们只是排序,这样箱子总是单调的,然后你可以使用pd.cut来分组它给出了很好的Inverval标签多索引可以被删除并有一个规则索引吗?然后信息就丢失了。记住,你有3个自由度均值或方差,低或高,级别2、3或5这就是为什么需要多索引,另一种选择是使用DataFrames字典@quant@ALollz可能这应该是一个单独的问题,但是否有可能获得截止值组内的平均值和std?因此,在这种情况下,将是组[2,3,5,2,2,3,3,5,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,[5,inf@quant呃,这已经足够接近了。类似的方法来获取值,我们只是排序,这样箱子总是单调的,然后你可以使用pd.cut来分组,它给出了很好的Inverval标签