Pandas,将最内部的索引转换为json字符串列或字典列表



对于前两个索引的每个唯一组合,我希望它们第三个索引的所有行(和索引名称(都转换为json字符串列。

例如


recs = [{'id':123,'color':'orange','store':'big_mart', 'price':6}, {'id':123,'color':'orange','store':'Buckyz','price':5}, {'id':234,'color':'blue', 'store':'Gmart','price':7}]
d3 = pd.DataFrame(recs)
d3.set_index(['id','color','store'])

我的预期输出应该是这样的。

idcolornew_col
123"范围">";[{‘store’:‘big_mart’,‘price’:6},{‘store':‘Buckyz’,‘price’:5}]">
234"蓝">";[{‘商店’:‘Gmart’,‘价格’:7

您可以groupby"id";以及";颜色";然后应用方向参数设置为"0"的CCD_ 2;记录";每组:

out = d3.groupby(['id', 'color'])[['store', 'price']].apply(lambda x: x.to_dict('records')).astype(str).reset_index(name='new_col')

或使用to_json(保存转换为str类型(:

out = d3.groupby(['id', 'color'])[['store', 'price']].apply(lambda x: x.to_json(orient='records')).reset_index(name='new_col')

输出:

id   color                                            new_col
0  123  orange  [{'store': 'big_mart', 'price': 6}, {'store': 'Buckyz', 'price': 5}]
1  234    blue                   [{'store': 'Gmart', 'price': 7}]

试试这个:

cols = ['id', 'colors']
new_df = d3.groupby(cols).apply(lambda x: x.drop(cols, axis=1).to_dict('records')).reset_index(name='new_col')

输出:

>>> new_df
id   color                                            new_col
0  123  orange  [{'store': 'big_mart', 'price': 6}, {'store': ...
1  234    blue                   [{'store': 'Gmart', 'price': 7}]

最新更新