pandas groupby 并转换为定义模式的 json

pandas groupby and convert to json of defined schema

我有以下 pandas df :

id  mobile
1   9998887776
2   8887776665
1   7776665554
2   6665554443
3   5554443332

我想按 id 和预期结果分组,如下所示:

id   mobile
1    [{"9998887776": {"status": "verified"}},{"7776665554": {"status": "verified"}}]
2    [{"8887776665": {"status": "verified"}},{"6665554443": {"status": "verified"}}]
3    [{"5554443332": {"status": "verified"}}]

我知道 to_json 方法在这里无济于事,我必须编写 UDF。但我是新手,有点卡在这里。

将列表理解与 GroupBy.apply 结合使用,并为字典列表自定义格式:

f = lambda x: [{y: {"status": "verified"}} for y in x]
df = df.groupby('id')['mobile'].apply(f).reset_index()
print (df)
   id                                             mobile
0   1  [{9998887776: {'status': 'verified'}}, {777666...
1   2  [{8887776665: {'status': 'verified'}}, {666555...
2   3             [{5554443332: {'status': 'verified'}}]

如需json格式:

import json

f = lambda x: json.dumps([{y: {"status": "verified"}} for y in x])
df = df.groupby('id')['mobile'].apply(f).reset_index()
print (df)
   id                                             mobile
0   1  [{"9998887776": {"status": "verified"}}, {"777...
1   2  [{"8887776665": {"status": "verified"}}, {"666...
2   3           [{"5554443332": {"status": "verified"}}]