【发布时间】:2017-06-04 11:14:07
【问题描述】:
在上一篇文章中,我曾要求使用 Python 中的字典从数据帧构造序列。
Construct sequences from a dataframe using dictionaries in Python
我想将答案中提出的功能更改为只有列表中的项目没有日期。
拥有:
{1: [1, 2], 2: [3, 1,], 4: [5, 3, 1]}
而不是:
{1: [[1, 'date_1'], [2, 'date_2']],
2: [[3, 'date_1'], [1, 'date_3']],
4: [[5, 'date_2'], [3, 'date_3'], [1, 'date_5']]}
改变功能:
fnc = lambda x: x.sort_values('date').values.tolist()
df.set_index('users').groupby(level=0).apply(fnc).to_dict()
【问题讨论】:
标签: python function pandas dictionary dataframe