需要将name和list两列用-符号连接起来
data = {'月':[1,1,2,2,2,3,1,3,4],
'name':['a','','b','b','a','c','c','c','c'],
'list':['纸','笔','书','纸','笔','书','书','笔','']}
df = pd.DataFrame(data)
月 name list
0 1 a 纸
1 1 笔
2 2 b 书
3 2 b 纸
4 2 a 笔
5 3 c 书
6 1 c 书
7 3 c 笔
8 4 c
执
df['name-list']=df[['name','list']].agg('-'.join,axis=1)
因为第二行和最后一行有空值,结果会显示'-笔','c-',而不是预期的'笔'和'c'