简体   繁体   English

python pandas - 在groupby之后选择特定值

[英]python pandas - select particular values after groupby

I have groupby table: 我有groupby表:

df.groupby(['Age', 'Movie']).mean()

                  User  Raitings
Age Movie
1   1         4.666667  7.666667
    2         4.666667  8.000000
    3         2.000000  7.500000
    4         2.000000  5.500000
    5         3.000000  7.000000
18  1         3.000000  7.500000
    2         3.000000  8.000000
    3         3.000000  8.500000
25  1         8.000000  7.250000
    2         8.000000  7.500000
    3         5.500000  8.500000
    4         5.000000  7.000000
45  1         9.000000  7.500000
    2         9.000000  7.500000
    3        11.000000  7.000000
    4        11.000000  6.000000
60  1         8.000000  7.000000
    2         8.000000  9.000000
    3         8.000000  7.000000

please, help with function, which takes integer (Age) and return Movie with MIN raitings in this Age-group. 请帮助函数,它接受整数(年龄)并在此Age-group中返回带有MIN raitings的电影。 Example def(1) should return 4 (min Raitings in group Age(1) = 5.5, Movies(5.5) = 4) 示例def(1)应返回4(组Age(1)中的最小值Raitings = 1,电影(5.5)= 4)

I can get min Raiting: 我可以得到最小的Raiting:

df['Raitings'].min()

But i don't know - how to get raiting in particular group (Age)? 但我不知道 - 如何在特定群体(年龄)获得raiting?

Source multi-index DF: 来源多指数DF:

In [221]: x
Out[221]:
                 User  Raitings
Age  Movie
1.0  1       4.666667  7.666667
     2       4.666667  8.000000
     3       2.000000  7.500000
     4       2.000000  5.500000
     5       3.000000  7.000000
18.0 1       3.000000  7.500000
     2       3.000000  8.000000
     3       3.000000  8.500000
25.0 1       8.000000  7.250000
     2       8.000000  7.500000
     3       5.500000  8.500000
     4       5.000000  7.000000
45.0 1       9.000000  7.500000
     2       9.000000  7.500000
     3      11.000000  7.000000
     4      11.000000  6.000000
60.0 1       8.000000  7.000000
     2       8.000000  9.000000
     3       8.000000  7.000000

Function: 功能:

In [222]: def f(df, age):
     ...:     return df.loc[pd.IndexSlice[age,:], 'Raitings'].idxmin()[1]
     ...:

Test: 测试:

In [223]: f(x, age=1)
Out[223]: 4

This gets all of them in one go. 这样就可以一次性完成所有这些工作。

df.groupby('Age').Raitings.idxmin().str[-1]

Age
1     4
18    1
25    4
45    4
60    1
Name: Raitings, dtype: int64

If you want a function, I'd use pd.DataFrame.xs (xs is for cross section). 如果你想要一个函数,我会使用pd.DataFrame.xs (xs代表横截面)。
By default, xs will grab from the first level of the index and subsequently drop that level. 默认情况下, xs将从索引的第一级抓取并随后降低该级别。 This conveniently leaves the level in which we want to draw the value in which idxmin will hand us. 这方便地留下了我们想要绘制idxmin将给我们的值的水平。

def f(df, age):
    return df.xs(age).Raitings.idxmin()

f(df, 1)

4

Setup 设定
Useful for those who try to parse this stuff. 对于那些试图解析这些东西的人很有用。

txt = """\
Age  Movie       User  Raitings
1.0  1       4.666667  7.666667
     2       4.666667  8.000000
     3       2.000000  7.500000
     4       2.000000  5.500000
     5       3.000000  7.000000
18.0 1       3.000000  7.500000
     2       3.000000  8.000000
     3       3.000000  8.500000
25.0 1       8.000000  7.250000
     2       8.000000  7.500000
     3       5.500000  8.500000
     4       5.000000  7.000000
45.0 1       9.000000  7.500000
     2       9.000000  7.500000
     3      11.000000  7.000000
     4      11.000000  6.000000
60.0 1       8.000000  7.000000
     2       8.000000  9.000000"""

df = pd.read_fwf(pd.io.common.StringIO(txt))
df = df.ffill(downcast='infer').set_index(['Age', 'Movie'])

If you want the minimum for a specific age, you can do : 如果您想要特定年龄的最低要求,您可以:

df["Age"==1]['Raitings'].min()

If you want to do it for the whole dataframe, you can do: 如果要对整个数据帧执行此操作,可以执行以下操作:

df.groupby("Age").agg({ "Raitings" : "min" })

I hope it helps, 我希望它有所帮助,

I will reshape and do pivot. 我会重塑并做点。 Think it will help 认为这会有所帮助

df.reset_index(inplace = true)
df_Min = pd.pivot_table(df,index = [‘Movie’, ‘User’], columns =‘Age’, values = ‘Raiting’, aggfunc = min )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM