I want to be able to concatenate string on several lines into one according to an ID. I use the library pandas (python 3).
val id
Cat 1
Tiger 2
Ball 3
Bat 1
bill 2
dog 1
l = []
a = 0
while a < lendata:
if df["id"][a] == 1:
if a != 0:
df["val"][tmp] = ' '.join(l)
l = []
tmp = a
l.append(df["val"][a])
else:
l.append(df["val"][a])
a += 1
It works with loops. i need this result,
val
Cat Tiger Ball
Bat bill
dog
not a group by
Question: Do you know how to do it with pandas functions? Thanks.