计算平均值和标准差列并将其添加到数据框

时间:2017-11-06 21:45:54

标签: python pandas jupyter-notebook

我有:

struct Executor {
    std::map<work_id_t, std::promise<Result>> work_items;
};

struct Work {
    std::future<Result> ft;
    work_id_t id;
};

// run requests asynchronously
Work Executor::post_work(Request r, work_id_t id) {
    work_items[id] = std::promise<Result>();
    do_request(w);
    return { work_items[id].get_future(), id };
}

// called when a requests finishes (the server supplies the id)
void Executor::work_finished(work_id_t id, Result r) {
    work_items[id].set_value(r);
    work_items.erase(id);
}

// ...

Executor e;
Work w = e.post_work(Request("foo"));    
auto wait_result = w.ft.wait_for(timeout_value);
if (wait_result == std::future_status::timeout) {
    e.remove_item(w.id);
}

我想在数据框中添加2列,计算平均值和标准差,如:

df = pd.DataFrame({'A1': [0.1,0.5,3.0, 9.0], 'A2':[2.0,4.5,1.2,9.0]})

1 个答案:

答案 0 :(得分:3)

让我们尝试assign使用meanstd使用参数axis=1

df.assign(Mean=df.mean(1), Stddev=df.std(1))

输出:

    A1   A2  Mean    Stddev
0  0.1  2.0  1.05  1.343503
1  0.5  4.5  2.50  2.828427
2  3.0  1.2  2.10  1.272792
3  9.0  9.0  9.00  0.000000

编辑评论/添加CpK:

df.assign(mean=df.mean(1),stddev=df.std(1)).eval('Cpk = (mean +  stddev) / A2')

输出:

    A1   A2  mean    stddev       Cpk
0  0.1  2.0  1.05  1.343503  1.196751
1  0.5  4.5  2.50  2.828427  1.184095
2  3.0  1.2  2.10  1.272792  2.810660
3  9.0  9.0  9.00  0.000000  1.000000