<p>为了正确工作,必须匹配字典的所有键和值</p>
<p>Idea是通过字典的键选择所有列,通过<code>1</code>进行比较,并为通过<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.where.html" rel="nofollow noreferrer">^{<cd2>}</a>中的dictional值选择的匹配列重命名列:</p>
<pre><code>lookup_dictionary = {'delirium_sae' : 'delirium_date',
'syncope_sae' : 'syncope_date',
'tia_sae' : 'tia_date'}
mask = df[list(lookup_dictionary.keys())].eq(1).rename(columns=lookup_dictionary)
print (mask)
delirium_date syncope_date tia_date
0 True False False
1 False False False
2 False True False
3 False True False
4 False False True
5 False False False
6 True False False
df = df[['patient_id']].join(df[list(lookup_dictionary.values())].where(mask))
print (df)
patient_id delirium_date syncope_date tia_date
0 1 01-08-2020 NaN NaN
1 1 NaN NaN NaN
2 2 NaN 02-08-2020 NaN
3 2 NaN 02-08-2020 NaN
4 3 NaN NaN 04-08-2020
5 3 NaN NaN NaN
6 3 02-08-2020 NaN NaN
</code></pre>