回答ありがとうございます。
参考にさせていただきます。
Python
1def main(): 2 features_df = pd.DataFrame() 3 features_df['labels'] = [0,1,2,0,1] 4 features_df['T1'][0] = 1 5 features_df['T2'][0] = 1 6 print(features_df) 7 8if __name__ == '__main__': 9 main()
想定している結果 labels T1 T2 0 0 1 1 1 1 Nan Nan 2 2 Nan Nan 3 0 Nan Nan 4 1 Nan Nan
以上のコードを実行すると、
Traceback (most recent call last): File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2393, in get_loc return self._engine.get_loc(key) File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239) File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085) File "pandas/_libs/hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405) File "pandas/_libs/hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359) KeyError: 'T1' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "pa.py", line 94, in <module> main() File "pa.py", line 89, in main features_df['T1'][1] = 1 File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/frame.py", line 2062, in __getitem__ return self._getitem_column(key) File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/frame.py", line 2069, in _getitem_column return self._get_item_cache(key) File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 1534, in _get_item_cache values = self._data.get(item) File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/internals.py", line 3590, in get loc = self.items.get_loc(item) File "/Users/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2395, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239) File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085) File "pandas/_libs/hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405) File "pandas/_libs/hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359) KeyError: 'T1'
このようなエラーがでてしまいます。
原因はなにが考えられるでしょうか。
よろしくお願い致します。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/23 04:13