この形のデータを分析しようとしています...
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 を使用して、このデータ変換を実現するためのはるかに優れた方法があると確信しています。