按列分组,另一列连接

时间:2018-02-01 06:08:14

标签: pandas

对于以下数据框,

d = {'col1': [33,33,33,34,34,34], 'col2': ["hello","hello1","hello2","hello3","hello4","hello5"]}
df = pd.DataFrame(data=d)
print(d)

我希望它按col1分组,col2中的内容作为列表连接,结果如下:

import pandas as pd
d = {'col1': [33,34], 'col2': [["hello","hello1","hello2"],["hello3","hello4","hello5"]]}
df = pd.DataFrame(data=d)
print(d)

有没有一种简单的方法可以实现这一目标?

0 个答案:

没有答案