Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Pandas.仅在第二级多重索引上的loc_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 3.x Pandas.仅在第二级多重索引上的loc

Python 3.x Pandas.仅在第二级多重索引上的loc,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有一个多索引数据帧,我想从第一级索引中选择所有值(这里表示为x,从第二级选择一个特定索引,这里是分组后.descripe()函数的平均值) df.groupby(df1.series).describe().loc[*x*,'mean'] 我该怎么做 谢谢您可以使用元组选择: df = pd.DataFrame({'x':[4,5,7,6,10], 'series':list('xxxyy')}) b = df.x.groupby(df.series).describe().stack()

我有一个多索引数据帧,我想从第一级索引中选择所有值(这里表示为x,从第二级选择一个特定索引,这里是分组后.descripe()函数的平均值)

df.groupby(df1.series).describe().loc[*x*,'mean']
我该怎么做


谢谢

您可以使用元组选择:

df = pd.DataFrame({'x':[4,5,7,6,10], 'series':list('xxxyy')})
b = df.x.groupby(df.series).describe().stack()
print (b)
series       
x       count     3.000000
        mean      5.333333
        std       1.527525
        min       4.000000
        25%       4.500000
        50%       5.000000
        75%       6.000000
        max       7.000000
y       count     2.000000
        mean      8.000000
        std       2.828427
        min       6.000000
        25%       7.000000
        50%       8.000000
        75%       9.000000
        max      10.000000
dtype: float64

c = b.loc[('x','mean')]
print (c)
5.33333333333
如果您需要所有方式,请使用:


您可以使用元组来选择:

df = pd.DataFrame({'x':[4,5,7,6,10], 'series':list('xxxyy')})
b = df.x.groupby(df.series).describe().stack()
print (b)
series       
x       count     3.000000
        mean      5.333333
        std       1.527525
        min       4.000000
        25%       4.500000
        50%       5.000000
        75%       6.000000
        max       7.000000
y       count     2.000000
        mean      8.000000
        std       2.828427
        min       6.000000
        25%       7.000000
        50%       8.000000
        75%       9.000000
        max      10.000000
dtype: float64

c = b.loc[('x','mean')]
print (c)
5.33333333333
如果您需要所有方式,请使用:


你认为
df.groupby(df1.series).descripe().loc[(*x*,'mean')]
?你认为
df.groupby(df1.series).descripe().loc[(*x*,'mean')]
?如果我的答案有用,别忘了-点击复选标记(
)将答案从灰色切换为已填写。谢谢。如果我的答案有用,请不要忘记它-单击复选标记(
),将其从灰色切换为已填写。谢谢。