隔离(分组)每个号码的所有消息(两种方式)
Segregate (Group) all the messages of each number(two way)
我有 phone 个号码之间的对话数据
from phone
to phone
message
7788994455
2233665588
hi
2233665588
7788994455
hello
1122335566
4455117766
where are you
4455117766
1122335566
I am home
2233665588
7788994455
wassup?
我希望隔离每个号码的所有消息(两种方式)。
应该是:-
上面的例子table:
7788994455,2233665588:- 嗨|你好|wassup?
我正在寻找整个对话应该相应地分组(从 phone 到 phone)。
你可以这样做:
df['from_to'] = list(zip(df['from'], df['to']))
df['from_to'] = df['from_to'].apply(lambda x:tuple(sorted(x)))
df.groupby('from_to').agg({'message':'|'.join})
输出:
message
from_to
(1122335566, 4455117766) where are you|I am home
(2233665588, 7788994455) hi|hello|wassup?
只需为两者创建一个组合字段:
df['grouper'] = df.apply(lambda x: set([x['from phone'], x['to phone']]), axis=1).astype(str)
然后做一个 groupby
:
df.groupby('grouper').agg({'message': ' | '.join})
这将为您提供以下数据框:
message
grouper
{1122335566, 4455117766} I am home
{2233665588, 7788994455} hi | hello | wassup?
{4455117766, 1122335566} where are you
我有 phone 个号码之间的对话数据
from phone | to phone | message |
---|---|---|
7788994455 | 2233665588 | hi |
2233665588 | 7788994455 | hello |
1122335566 | 4455117766 | where are you |
4455117766 | 1122335566 | I am home |
2233665588 | 7788994455 | wassup? |
我希望隔离每个号码的所有消息(两种方式)。
应该是:-
上面的例子table:
7788994455,2233665588:- 嗨|你好|wassup?
我正在寻找整个对话应该相应地分组(从 phone 到 phone)。
你可以这样做:
df['from_to'] = list(zip(df['from'], df['to']))
df['from_to'] = df['from_to'].apply(lambda x:tuple(sorted(x)))
df.groupby('from_to').agg({'message':'|'.join})
输出:
message
from_to
(1122335566, 4455117766) where are you|I am home
(2233665588, 7788994455) hi|hello|wassup?
只需为两者创建一个组合字段:
df['grouper'] = df.apply(lambda x: set([x['from phone'], x['to phone']]), axis=1).astype(str)
然后做一个 groupby
:
df.groupby('grouper').agg({'message': ' | '.join})
这将为您提供以下数据框:
message
grouper
{1122335566, 4455117766} I am home
{2233665588, 7788994455} hi | hello | wassup?
{4455117766, 1122335566} where are you