catspeake
看来您的函数没有删除一次出现的条目(即该组的记录数)。如果你想保持句子中单词的顺序,你可以添加as_index=False, sort=Falseas 选项groupby,如果这就是你所说的“分组数据时保持句子顺序”的意思。import pandas as pdimport refrom collections import Counterdata = [['1', '1', '1', '1', '1', '1', '1'], ['a', 'a', 'a', 'a', 'school', 'is', 'closed'], ['Object', 'Object', 'Object', 'Object', 'Depart', 'Verb', 'O']]d = {'Sentence': data[0], 'Text': data[1], 'Classes': data[2]}df = pd.DataFrame(data=d)def md(s): c = Counter(s) return c.most_common(1)[0][0]df_final = df.groupby(['Sentence','Text'], as_index=False, sort=False).Classes.agg(md)print(df_final)输出: Sentence Text Classes0 1 a Object1 1 school Depart2 1 is Verb3 1 closed O