Now I have a dataframe, I want to merge rows. The value B is determined by the order in the strings in a list L = ['xx','yy','zz']
A B
0 a xx
1 a yy
2 b zz
3 b yy
Desired outcome:
A B
0 a xx
1 b yy
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.