答案 0 :(得分:1)
IIUC,np.sort
和Groupby.sum
print(df)
col1 col2 col3
0 A B 10
1 C D 15
2 B A 15
3 D C 3
cols = ['col1', 'col2']
new_df = (df.assign(**dict(zip(cols, np.sort(df[cols], axis=1).T)))
.groupby(cols, as_index=False).sum())
print(new_df)
col1 col2 col3
0 A B 25
1 C D 18
或
df2 = df.copy()
df2[cols] = np.sort(df[cols], axis=1)
df2.groupby(cols, as_index=False).sum()