#前提・実演したいこと
条件の組み合わせから、期待値を取り出し表示できるようにしたい。
例えば、表の表側(front_table)と表頭(table_head)、そして期待値として、expected_valueをそれぞれ下記のようにリストにまとめる。
python
1front_table = { 2 '操作':['電源ボタン','ビデオボタン','静止ボタン'] 3} 4 5table_head = { 6 '期待動作':['動作'] 7} 8 9expected_value = [ 10 ['電源ボタンを押す'], 11 ['ビデオボタンを押す'], 12 ['静止ボタンを押す'] 13]
そして表側と表頭の条件から下記のように真偽を網羅的に表示させる。
python
1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2電源ボタン T T T T T T T T F F F F F F F F 3ビデオボタン T T T T F F F F T T T T F F F F 4静止ボタン T T F F T T F F T T F F T T F F 5動作 T F T F T F T F T F T F T F T F 6``` 7実装したいことは、例えば、 8「電源ボタン」と「動作」が「T」でそれ以外が「F」だったら 99列目のところに 10``` 11 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12電源ボタン T T T T T T T T F F F F F F F F 13ビデオボタン T T T T F F F F T T T T F F F F 14静止ボタン T T F F T T F F T T F F T T F F 15動作 T F T F T F T F T F T F T F T F 16------------------------------------------------------------ 17アクション1 電源ボタンを押す 18``` 19のように対応させて表として表示させたい。 20残りの条件も 21・「ビデオボタン」と「動作」 22・「静止ボタン」と「動作」 23と対応している。 24 25出来れば、条件や期待値が変わっても対応できるように一般化したい。 26 27#行ったこと 28条件を与えて、真偽を網羅的に表示するところまでを行った。 29コードは下記の通り。 30```python 31#表側,表頭を指定して、それに対応した期待値を抽出 32class Table: 33 def __init__(self, row_names, column_names, data): 34 self.row_names = row_names 35 self.column_names = column_names 36 self.data = data 37 38 def get_datum(self, row_name, column_name): 39 row_index = self.row_names.index(row_name) 40 column_index = self.column_names.index(column_name) 41 42 return self.data[row_index][column_index] 43 44table = Table(front_table['操作'],table_head['期待動作'],expected_value) 45dat = table.get_datum('ビデオボタン','動作') 46 47print(dat) 48 49 50#真偽のマーク 51true_mark = 'T' 52false_mark = 'F' 53 54#条件(表側,表頭)に対しての真偽の組み合わせを表示 55def combination_TF(front_table,table_head): 56 table_index = front_table + table_head 57 58 #真偽の直積 59 cartesian_product = itertools.product(*([true_mark,false_mark],)*len(table_index)) 60 61 #真偽の直積の結果を表で表示 62 dataTF = [[] for element_count in table_index] 63 for com in cartesian_product: 64 for element_count,authenticity in enumerate(com): 65 dataTF[element_count].append(authenticity) 66 return pd.DataFrame(dataTF,index=table_index) 67 68print(combination_TF(front_table['操作'],table_head['期待動作'])) 69 70``` 71#実行結果 72``` 73ビデオボタンを押す 74 75 76 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 77電源ボタン T T T T T T T T F F F F F F F F 78ビデオボタン T T T T F F F F T T T T F F F F 79静止ボタン T T F F T T F F T T F F T T F F 80動作 T F T F T F T F T F T F T F T F 81``` 82長々と申し訳ありませんが、よろしくお願いします。 83上記のコードにあるクラスなどは無視しても構いません。 84 85とりあえず、条件の「T」と「F」の組み合わせから、期待値を取り出すのを一般化できればと思っています。 86 87#自分の解決方法 88自分の解決方法として、 89例えば、実行結果の下の方の表で「電源ボタン」と「動作」が'T'でそれ以外が'F'だった時にexpected_valueの中の「電源ボタンを押す」という期待値をその列上に出したい。 90 91プログラムとして、 92・列と行をfor文で回していき、「電源ボタン」と「動作」が'T'だったらその場所の列の 93位置(数値)を返す。 94 95例は以下のコードで実現しようと考えている。 96 97```python 98expected_data = [[] for j in range(len(df.index))] 99for index_number in range(len(df.index)): 100 for columns_number in range(len(df.columns)): 101 if df[columns_number][index_number] == 'T': 102 index_number += len(df.index) - 1 103 if df[columns_number][index_number] == 'T': 104 expected_data[index_number].append(columns_number) 105print(expected_data) 106``` 107#実行結果 108Traceback (most recent call last): 109 File "C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2428, in get_value 110 tz=getattr(series.dtype, 'tz', None)) 111 File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4363) 112 File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4046) 113 File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085) 114 File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405) 115 File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359) 116KeyError: 4 117 118During handling of the above exception, another exception occurred: 119 120Traceback (most recent call last): 121 File "C:\atom\decision.py", line 72, in <module> 122 if df[columns_number][index_number] == 'T': 123 File "C:\Anaconda\lib\site-packages\pandas\core\series.py", line 601, in __getitem__ 124 result = self.index.get_value(self, key) 125 File "C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2434, in get_value 126 return libts.get_value_box(s, key) 127 File "pandas\_libs\tslib.pyx", line 923, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18843) 128 File "pandas\_libs\tslib.pyx", line 939, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18560) 129IndexError: index out of bounds 130 131実行結果はエラーが出ているので、教えてほしいと思っています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。