移动groupby对象,但也保留原始数据

时间:2018-08-14 07:55:50

标签: python pandas dataframe pandas-groupby

var array =[{
        "_id": "5b7229fd5fd0b77763cd5d0b",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229d15fd0b77763cd5adc",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229c45fd0b77763cd5a11",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229be5fd0b77763cd59b9",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229be5fd0b77763cd59bf",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229c15fd0b77763cd59fb",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    ];

    var array2 = [
    {"_id": "5b7229c15fd0b77763cd59fb"},
    {"id":"5b7229be5fd0b77763cd59bf"},
    {"id":"5b7229be5fd0b77763cd59b9"}
    ];
    varResultArray = [{
        "_id": "5b7229fd5fd0b77763cd5d0b",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229d15fd0b77763cd5adc",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229c45fd0b77763cd5a11",
        "title": "Nexus of knowledge",
        "isRecommended":false,
       },
    {
        "_id": "5b7229be5fd0b77763cd59b9",
        "title": "Nexus of knowledge",
        "isRecommended":true,
       },
    {
        "_id": "5b7229be5fd0b77763cd59bf",
        "title": "Nexus of knowledge",
        "isRecommended":true,
       },
    {
        "_id": "5b7229c15fd0b77763cd59fb",
        "title": "Nexus of knowledge",
        "isRecommended":true,
       },
    ];

我想根据它们的顺序在每种类型中获取滞后值,我也想保留原始值。我想要达到的结果是:

TYPE ORDER Value
A    1      2
A    2      3
A    3      4
B    1      3
A    4      5
B    2      6

如何完成?我知道我可以很容易地得到TYPE ORDER Value_Lag1 Value A 1 NAN 2 A 2 2 3 A 3 3 4 A 4 4 5 B 1 NAN 3 B 2 3 6 groupby的滞后,但是我对如何保持原始值也感到困惑。 我想到的唯一方法是shift和原始concat以及转移后的df。有没有更容易实现的目标?

0 个答案:

没有答案