You can use pandas.Series.map
and pandas.DataFrame.groupby
:
df['C'] = df['B'].map(dict(zip(L,range(len(L)))))
df.groupby('A')[['B','C']].apply(lambda x: x.iloc[x["C"].argmin()]['B'])
#A
#a xx
#b yy
You can get the same result using pandas.Categorical
:
df['B'] = pd.Categorical(df['B'], categories = L, ordered = True)
df.groupby('A').min()
# B
#A
#a xx
#b yy
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…