你可以使用
pd.groupby
. 输出看起来很难看,但它应该是您需要的
a = df.groupby([df.is_from_me.diff().ne(0).cumsum()]).agg(lambda x: tuple(x))
a['output'] = a['text']
a['input'] = a.shift()['text']
输出
input \
is_from_me
1 NaN
2 (Happy birthday bud!!!,)
3 (Thanks man!,)
4 (Definitely would've come back had I thought a...
5 (Your good,)
6 (Okay haha, Have a good one)
7 (Yea you too. What are you up to?,)
8 (No hw like I'm doing all day, Just got up)
9 (Same here. I went to the football game last...,)
10 (I think I saw that in your story, Win?)
11 (Lost in last second,)
12 (Aw, that sucks, Means it was a good game tho?)
13 (Really good game. They were on the 1/2 yard l...
output
is_from_me
1 (Happy birthday bud!!!,)
2 (Thanks man!,)
3 (Definitely would've come back had I thought a...
4 (Your good,)
5 (Okay haha, Have a good one)
6 (Yea you too. What are you up to?,)
7 (No hw like I'm doing all day, Just got up)
8 (Same here. I went to the football game last...,)
9 (I think I saw that in your story, Win?)
10 (Lost in last second,)
11 (Aw, that sucks, Means it was a good game tho?)
12 (Really good game. They were on the 1/2 yard l...
13 (Dang,)