Short answer
data = pd.DataFrame([[1,1,'Hi'],[1,1,'my name is'], [1,1,'Hal'],[1,1,'my name is'], [1,1,'Hal'],[2,0,'Ich bin'], [2,0,'ein kartoffeln'],[2,0,'!'], [2,0,'ein kartoffeln'],[2,0,'!'],[1,1,'my name is'], [1,1,'Obama'],[1,1,'president of USA'], [1,1,'Obama'],[1,1,'president of USA'],[2,0,'Hi'],[2,0,'my name is'], [2,0,'James webb'],[2,0,'my name is'], [2,0,'James webb'],[1,1,'Ich bin'], [1,1,'ein potatoe'], [1,1,'hello human'],[1,1,'ein potatoe'], [1,1,'hello human']])
data.columns = ['id', 'gender', 'tweet']
n = 5
block = int(round(len(data)/n, 0))
data['block'] = np.repeat(range(1, block+1), n)
data_block = data.groupby(['id','gender', 'block'])['tweet'].agg(lambda x: '-'.join(x.dropna())).reset_index()
data_block
Long answer : Full explanations
Creating fake data :
data = pd.DataFrame([
[1,1,'Hi'],[1,1,'my name is'], [1,1,'Hal'],
[2,0,'Ich bin'], [2,0,'ein kartoffeln'],[2,0,'!'],
[1,1,'my name is'], [1,1,'Obama'],[1,1,'president of USA'],
[2,0,'Hi'],[2,0,'my name is'], [2,0,'James webb'],
[1,1,'Ich bin'], [1,1,'ein potatoe'], [1,1,'hello human']
])
data.columns = ['id', 'gender', 'tweet']
Warning : Assuming there is the right number of repetions per id and your data are sorted in the right way.
==> If not you can sort data by user id and isolate it as independent dataframe while truncating it if there is not the right number of row (multiple of what you want). Then you merge it again as one. Care of the context of your analysis/work of course :)
I defined the Number of block of repeated value you want - In your case groups of n=5. I used n=3 for the example.
n = 3
block = int(round(len(data)/n, 0))
data['block'] = np.repeat(range(1, block+1), n)
data_block = data.groupby(['id','gender', 'block'])['tweet'].agg(lambda x: '-'.join(x.dropna())).reset_index()
It gives :
>>> data_block
id gender block tweet
0 1 1 1 Hi-my name is-Hal
1 1 1 3 my name is-Obama-president of USA
2 1 1 5 Ich bin-ein potatoe-hello human
3 2 0 2 Ich bin-ein kartoffeln-!
4 2 0 4 Hi-my name is-James webb
Is it better ?
Straight example with group of 5 it works also well :
data = pd.DataFrame([
[1,1,'Hi'],[1,1,'my name is'], [1,1,'Hal'],[1,1,'my name is'], [1,1,'Hal'],
[2,0,'Ich bin'], [2,0,'ein kartoffeln'],[2,0,'!'], [2,0,'ein kartoffeln'],[2,0,'!'],
[1,1,'my name is'], [1,1,'Obama'],[1,1,'president of USA'], [1,1,'Obama'],[1,1,'president of USA'],
[2,0,'Hi'],[2,0,'my name is'], [2,0,'James webb'],[2,0,'my name is'], [2,0,'James webb'],
[1,1,'Ich bin'], [1,1,'ein potatoe'], [1,1,'hello human'], [1,1,'ein potatoe'], [1,1,'hello human']
])
data.columns = ['id', 'gender', 'tweet']
n = 5
block = int(round(len(data)/n, 0))
data['block'] = np.repeat(range(1, block+1), n)
data_block = data.groupby(['id','gender', 'block'])['tweet'].agg(lambda x: '-'.join(x.dropna())).reset_index()
data_block
>>> data_block
id gender block tweet
0 1 1 1 Hi-my name is-Hal-my name is-Hal
1 1 1 3 my name is-Obama-president of USA-Obama-presid...
2 1 1 5 Ich bin-ein potatoe-hello human-ein potatoe-he...
3 2 0 2 Ich bin-ein kartoffeln-!-ein kartoffeln-!
4 2 0 4 Hi-my name is-James webb-my name is-James webb