Python pandas:按日期索引和公共列值组合两个数据帧

时间:2017-08-04 10:05:57

标签: python python-2.7 pandas inner-join

有两个日期框架,一个是df1,另一个是df2,如下所示:

DF1:

               a   b    id
2010-01-01     1   4    21
2010-01-01     2   5    22
2010-01-01     3   6    23
2010-01-01     4   7    24
2010-01-02     1   4    21
2010-01-02     2   5    22
2010-01-02     3   6    23
2010-01-02     4   7    24
2010-01-03     1   4    21
2010-01-03     2   5    22
2010-01-03     3   6    23
2010-01-03     4   7    24
...........................

DF2:

               c   d    id
2010-01-02     1   4    21
2010-01-02     2   5    22
2010-01-02     3   6    23
2010-01-02     4   7    24
2010-01-03     1   4    21
2010-01-03     2   5    22
2010-01-03     3   6    23
2010-01-03     4   7    24
...........................

我想通过公共索引合并或加入两个数据帧(请注意df1中的某些索引不在df2中)和id,我希望加入的数据帧如下所示

               c   d    a   b   id
2010-01-02     1   4    1   4   21
2010-01-02     2   5    2   5   22
2010-01-02     3   6    3   6   23
2010-01-02     4   7    4   7   24
2010-01-03     1   4    1   4   21
2010-01-03     2   5    2   5   22
2010-01-03     3   6    3   6   23
2010-01-03     4   7    4    7  24

我使用了以下代码

 df = df1.join(df2, on = ['id'], how='inner')

但这不起作用

1 个答案:

答案 0 :(得分:3)

IIUC:

module.exports = function (callback) {
var request = require("request");
var getResponse = null;

request.get({
    url: '/Home/testReport',
    json: true,
}, function (error, response, body) {
    if (!error && response.statusCode == 200)
        getResponse = body;
    });

var jsreport = require('jsreport-core')();

jsreport.init().then(function () {
    return jsreport.render({
        template: {
            content: 'template bla  bla bla',
            engine: 'jsrender',
            recipe: 'phantom-pdf'
        },
        data: getResponse
    }).then(function (resp) {
        callback(/* error */ null, resp.content.toJSON().data);
    });
}).catch(function (e) {
    callback(/* error */ e, null);
});