【问题标题】:GroupBy Two Columns and then find Min of Third Column in PandasGroupBy Two Columns and then find Min of Third Column in Pandas
【发布时间】:2022-12-02 03:12:15
【问题描述】:

I have a dataset that looks like this:

SUBJECTID   session    value  attempt
  1          home:1:1    5      1
  1          home:2:1    2      1
  1          home:2:2    4      2

For every SUBJECTID and every session I want to only include the data for the lowest number attempt (note this isn't always attempt 1)

I've tried the following in pandas with no luck

group_a = totaltimes_a.groupby('SUBJECT')['session']
min_value = group_a.attempt.min()
totaltimes_a = totaltimes_a.merge(min_value, on='session',suffixes=('', '_min'))
totaltimes_a = totaltimes_a[totaltimes_a.attempt==totaltimes_a.attempt_min].drop('attempt_min', axis=1)

【问题讨论】:

    标签: pandas dataframe group-by minimum


    【解决方案1】:

    I hope I've understood your question right:

    df["attempt_min"] = df.groupby(["SUBJECTID", "session"])["attempt"].transform(
        "min"
    )
    print(df)
    

    Prints:

       SUBJECTID   session  value  attempt  attempt_min
    0          1  home:1:1      5        1            1
    1          1  home:2:1      2        1            1
    2          1  home:2:2      4        2            2
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      • 2021-12-30
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 2022-12-28
      • 2022-12-01
      相关资源
      最近更新 更多