【问题标题】:How to check different rows values of a column within the same group and return a specific value?如何检查同一组中列的不同行值并返回特定值?
【发布时间】:2022-11-08 11:56:49
【问题描述】:

我有以下代码生成两列。

import pandas as pd
  
data = {'Group': ['1', '1', '1', '1', '1', '1',
                  '2', '2', '2', '2', '2', '2',
                  '3', '3', '3', '3', '3', '3',
                  '4', '4', '4', '4', '4', '4',],
        'Test1': ['ABC', 'CDE', 'EFG', 'GHI', 'IJK', 'KLM',
                  'MNO', 'OPQ', 'QRS', 'STU', 'UVW', 'WXYZ',
                  'ABC', 'CDE', 'EFG', 'GHI', 'IJK', 'KLM',
                  'MNO', 'OPQ', 'QRS', 'STU', 'UVW', 'WXYZ',],
        'Test2': ['1234','4567', '8910', '1112', '1314', '1415',
                  '1516', '1718', '1920', '2122', '2324', '2526',
                  '2728', '2930', '3132', '3334', '3536', '3738',
                  '2940', '4142', '4344', '4546', '4748', '4950'],
        'Value': [True, True, False, False, False, True,
                  True, True, True, True, True, True,
                  True, True, True, True, True, False,
                  True, True, True, False, True, True,],
        }
  
df = pd.DataFrame(data)

print(df)

因此,通过检查每组中的最后 2、3 或 4 行是否返回 False,我想返回 False。如果所有的值都为真,那么我想为所有行返回真。从上面的代码来看,预期的结果是这样的。如果我们检查每组中的最后 3 行

Group | Value
----- | -----  
  1   |   False 
  1   |   False
  1   |   False
  2   |   True
  2   |   True
  2   |   True
  3   |   False
  3   |   False
  3   |   False
  4   |   False
  4   |   False
  4   |   False

【问题讨论】:

    标签: python pandas dataframe group-by data-science-experience


    【解决方案1】:

    更新,每个更新的问题和下面的 cmets:

    df[['Test1','Test2']].merge(df.groupby('Group')['Value'].apply(lambda x: x.iloc[-3:].mul(x.iloc[-3:].min(), level=0))
      .reset_index(), left_index=True, right_on='level_1').drop('level_1', axis=1)
    

    输出:

       Test1 Test2 Group  Value
    0    GHI  1112     1  False
    1    IJK  1314     1  False
    2    KLM  1415     1  False
    3    STU  2122     2   True
    4    UVW  2324     2   True
    5   WXYZ  2526     2   True
    6    GHI  3334     3  False
    7    IJK  3536     3  False
    8    KLM  3738     3  False
    9    STU  4546     4  False
    10   UVW  4748     4  False
    11  WXYZ  4950     4  False
    

    IIUC,试试这个:

    df.groupby('Group')['Value'].apply(lambda x: x.iloc[-3:].mul(x.iloc[-3:].min(), level=0))
      .reset_index()
      .drop('level_1', axis=1)
    

    输出:

       Group  Value
    0      1  False
    1      1  False
    2      1  False
    3      2   True
    4      2   True
    5      2   True
    6      3  False
    7      3  False
    8      3  False
    9      4  False
    10     4  False
    11     4  False
    

    【讨论】:

    • 嗯,聪明:按组分组,取最后 3 个条目,每个条目乘以最小值。因此,如果这三个中的任何一个为零,则它们都将归零。确保 True 只有在它们都为 True 时才会发生的好方法。一个问题:level = 0 这里是做什么的?我无法从.mul() 文档中看出这一点。
    • 嗨,我收到IndexError: single positional indexer is out-of-bounds。在GroupValue 之间还有其他列。如果我们还有其他列,我们如何修复代码?
    • @BadCoder 你能生产一个产生这个错误的数据集吗?
    • Mul 和 line up level = 0 的数据帧索引,在 groupby 中你会得到一个多索引,min 会给出单级索引,你告诉 mult 将单索引 pd.Series 与 level=0 的索引对齐其他 pd.Series。
    • @ScottBoston 我已经编辑了这个问题,它只返回了 Group 和 Value 列。我想知道使用更新的列从数据框中返回所有列
    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 2012-01-27
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多