我有一个像这样的数据框
| | Sentence | Text | Classes
0 1 a Object
1 1 a Object
2 1 a Object
3 1 a Object
4 1 school Depart
5 1 is Verb
6 1 closed O
.
.
. 60 a Verb
我想按班级最频繁的类型对文本进行分组,所以这样结束:
| | Sentence | Text | Classes
0 1 a Object
1 1 is Verb
2 1 school Depart
3 1 closed O
对数据进行分组时,保持句子顺序非常重要,当我尝试以下代码时,它会对数据进行分组,但是只删除一次:
def md(s):
c = Counter(s)
return c.most_common(1)[0][0]
df_final = df.groupby(['Sentence','Text']).Classes.agg(md)
答案 0 :(得分:0)
您的函数似乎没有删除单次出现的条目(即该组的记录数)。
如果要保持句子中单词的顺序,可以将as_index=False, sort=False
作为选项添加到groupby
,如果这是“在对数据进行分组时保留句子顺序”的意思, “。
import pandas as pd
import re
from collections import Counter
data = [['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 Classes
0 1 a Object
1 1 school Depart
2 1 is Verb
3 1 closed O