我正在尝试分析具有这种形式的数据...
date     | result | test
----------------------------
10-08-13 | True   | test_1
10-08-13 | True   | test_2
10-08-13 | False  | test_2
10-07-13 | True   | test_3
10-07-13 | False  | test_4
10-06-13 | True   | test_3
10-05-13 | False  | test_1
我要创建的是每个测试随时间推移的通过百分比的时间序列。所以理想情况下,我想将数据重新排列成这种形式:
date     | test_1 | test_2  | test_3  | test_4
-----------------------------------------------
10-08-13 | 50     | 70      | 55      | 100
10-08-13 | 60     | 70      | 55      | 100
10-08-13 | 30     | 70      | 55      | NaN
10-07-13 | 50     | 10      | NaN     | 100
10-07-13 | 30     | 10      | NaN     | 100
10-06-13 | 50     | 70      | Nan     | 100
10-05-13 | 50     | 70      | 55      | 100
到目前为止,我已经能够使用以下代码重新排列数据:
all_tests = data.groupby('test').size()
data_grouped = data.groupby('date')
per_test_per_day = {}
def tests_per_day(group):
    g = group.groupby('test')
    tests = g.size() 
    tests_pass = g['result'].sum()
    for d in all_tests.index:
    if d not in per_test_per_day:
        per_device_per_day[d] = []
    if d in tests:
        per_test_per_day[d].append(tests_pass[d] / tests[d] * 100)
    else:
        per_test_per_day[d].append(NaN)
data_grouped.apply(tests_per_day)
生成的数据框看起来与我需要的很接近,只是它的索引只是标准整数,所以我的 X 轴没有标有正确的日期。
我确信使用 Pandas 和 NumPy 有更好的方法来完成这种数据转换。