The @rfan answer, of course, works, as an alternative, the approach using pandas groupby is used here .
.groupby()groups data by column "b" - for sort=Falseyou need to keep order. .apply()applies a function to each group of b data, in this case, the union of the string together is separated by spaces.
In [67]: df.groupby('b', sort=False)['a'].apply(' '.join)
Out[67]:
b
DT The
Org Skoll Foundation
, ,
VBN based
IN in
Location Silicon Valley
Name: a, dtype: object
EDIT:
( ) - -, , , :
df['key'] = (df['b'] != df['b'].shift(1)).astype(int).cumsum()
, . , :
df = DataFrame({'a': ['The', 'Skoll', 'Foundation', ',',
'based', 'in', 'Silicon', 'Valley', 'A', 'Foundation'],
'b': ['DT', 'Org', 'Org', ',', 'VBN', 'IN',
'Location', 'Location', 'Org', 'Org']})
groupby:
In [897]: df.groupby(['key', 'b'])['a'].apply(' '.join)
Out[897]:
key b
1 DT The
2 Org Skoll Foundation
3 , ,
4 VBN based
5 IN in
6 Location Silicon Valley
7 Org A Foundation
Name: a, dtype: object