python - 如何在 pandas 中将 .loc 与 groupby 和两个条件一起使用

标签 python pandas

我问过类似的问题here ,但我想扩展这个问题,因为我被要求在我不能使用 .duplicates() 的地方做一些不同的事情

我有一个按“Key”分组的 df。我想标记出院日期与入院日期匹配的组中的任何行,并且在这些行之间,出院日期的行的 num1 值在 5-12 范围内。

df =  pd.DataFrame({'Key': ['10003', '10003', '10003', '10003', '10003','10003','10034', '10034'], 
   'Num1': [12,13,13,13,12,13,15,12],
   'Num2': [121,122,122,124,125,126,127,128],
  'admit': [20120506, 20120508, 20121010,20121010,20121010,20121110,20120520,20120520],  'discharge': [20120508, 20120510, 20121012,20121016,20121023,20121111,20120520,20120520]})
df['admit'] = pd.to_datetime(df['admit'], format='%Y%m%d')
df['discharge'] = pd.to_datetime(df['discharge'], format='%Y%m%d')

初始df

    Key     Num1    Num2    admit       discharge
0   10003   12      121     2012-05-06  2012-05-08
1   10003   13      122     2012-05-08  2012-05-10
2   10003   13      122     2012-10-10  2012-10-12
3   10003   13      124     2012-10-10  2012-10-16
4   10003   12      125     2012-10-10  2012-10-23
5   10003   13      126     2012-11-10  2012-11-11
6   10034   15      127     2012-05-20  2012-05-20
7   10034   12      128     2012-05-20  2012-05-20

最终目标

    Key     Num1    Num2    admit       discharge   flag
0   10003   12      121     2012-05-06  2012-05-08  1
1   10003   13      122     2012-05-08  2012-05-10  1
2   10003   13      122     2012-10-10  2012-10-12  0
3   10003   13      124     2012-10-10  2012-10-16  0
4   10003   12      125     2012-10-10  2012-10-23  0
5   10003   13      126     2012-11-10  2012-11-11  0
6   10034   15      127     2012-05-20  2012-05-20  1
7   10034   12      128     2012-05-20  2012-05-20  1

我正在尝试使用 filter(),但我不太清楚如何将 any() 应用于出院日期。我的逻辑是选择一组中的第一个入院日期,然后在每个出院日期中检查该日期,一旦匹配,然后检查具有相同出院日期的行是否在 Num1 中具有 5-12 范围内的值.

num1_range = [5,6,7,8,9,10,11,12]
df.loc[df.groupby(['Key']).filter(lambda x : (x['admit'] == x['discharge'].any())&(x['Num1'].isin(num1_range).any())),'flag']=1

我遇到了一个错误

ValueError: cannot set a Timestamp with a non-timestamp

最佳答案

我相信您正在寻找满足 flag = True 的 2 个条件之一:

  1. 入院日期等于组内任何出院日期(Key)。
  2. 出院日期等于组内任何入院日期,前提是Num1 在 5 到 12 之间(含)。

以下逻辑会产生符合您所需输出的结果。

解决方案

d1 = df.groupby('Key')['admit'].apply(set).to_dict()
d2 = df.groupby('Key')['discharge'].apply(set).to_dict()

def flagger(row):
    match1, match2 = row['discharge'] in d1[row['Key']], row['admit'] in d2[row['Key']]
    return match2 or (match1 and (row['Num1'] in range(5, 13)))

df['flag'] = df.apply(flagger, axis=1).astype(int)

结果

     Key  Num1  Num2      admit  discharge  flag
0  10003    12   121 2012-05-06 2012-05-08     1
1  10003    13   122 2012-05-08 2012-05-10     1
2  10003    13   122 2012-10-10 2012-10-12     0
3  10003    13   124 2012-10-10 2012-10-16     0
4  10003    12   125 2012-10-10 2012-10-23     0
5  10003    13   126 2012-11-10 2012-11-11     0
6  10034    15   127 2012-05-20 2012-05-20     1
7  10034    12   128 2012-05-20 2012-05-20     1

解释

  • 分别创建 2 个字典映射 Key -> Admit dates 和 Key -> Discharge dates。
  • 使用这 2 个字典应用使用 pd.DataFrame.apply 的行指定的条件。

关于python - 如何在 pandas 中将 .loc 与 groupby 和两个条件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49164935/

相关文章:

python - Selenium 错误 : AttributeError: 'Service' object has no attribute 'process'

Python 和 PySide : Variable values reset when calling functions from another file

python - Pandas:如果满足条件,则用前一行值替换列的值

python - 地理错误: GeocoderServiceError: HTTP Error 500: Internal Server Error using pandas apply function with str concat

python - 相机重新对焦时Opencv读取图像(仍然模糊)

python - 将 XML 文件内容发送到事件中心并从 Databricks 读取它

python - 如何从 Anaconda 环境构建 .exe 程序 (Python 3.7)

python - 如何通过查找其他列中的非空值来创建列

python - Pandas 相关矩阵与 value_counts 列字符串

python-3.x - 使用 Pandas 过滤具有多个值的单元格中的字符串