背景需求:
题目查重,需合并选项。现已经获得目标题目ID以及选项,
目标:相同question_id,合并选项
思路:
data=df.groupby(by='question_id')['choice'].sum()
print(data)
报错:
原因:
choice中有空致存在,最后更改如下:
df=pd.read_csv('choices.csv')
df=df.fillna(" ")
data=df.groupby(by='question_id')['choice'].sum()
print(data)
总结:
df=pd.read_csv('choices.csv') df=df.fillna(" ") data=df.groupby(by='question_id')['choice'].sum() print(data.head()) data1=df.groupby(by='question_id').apply(lambda x:[','.join(x['choice'])]) print(data1.head()) data1=df.groupby(by='question_id').apply(lambda x:[''.join(x['choice'])]) print(data1.head()) data1=df.groupby(by='question_id').apply(lambda x:''.join(x['choice'])) print(data1.head())