<p>我想如果你真的想使用列表理解,你可以,但是@yatu的解决方案会更快:</p>
<pre><code># unstack and find the first column index where relationship or single occurs
first = df.unstack().groupby(level=1).apply(lambda x: (np.isin(x.values, ['relationship', 'single'])).argmax())
last = df.unstack()[::-1].groupby(level=1).apply(lambda x: (np.isin(x.values, ['relationship', 'single'])).argmax())
# list comprehension to find the index and column index pair
f_list = [x for x in enumerate(first)]
l_list = [x for x in enumerate(last)]
# list comprehension with iloc
f_val = [df.iloc[f_list[i]] for i in range(len(f_list))]
l_val = [df.loc[:, ::-1].iloc[l_list[i]] for i in range(len(l_list))]
# create columns
df['first'] = f_val
df['last'] = l_val
Client_ID Date_1 Date_2 Date_3 Date_4 \
0 C1019876 relationship no change no change no change
1 C1018765 no change single no change no change
2 C1017654 single no change relationship NaN
3 C1016543 NaN relationship no change single
4 C1015432 NaN no change single NaN
first last
0 relationship relationship
1 single single
2 single relationship
3 relationship single
4 single single
</code></pre>
<p>时间结果:<code>8 ms ± 230 µs per loop (mean ± std. dev. of 3 runs, 1000 loops each)</code></p>