前提・実現したいこと
"Pythonではじめるスタートブック"のサンプルコードのエラーです。
発生している問題・エラーメッセージ
Ptyhon
1TypeError Traceback (most recent call last) 2<ipython-input-21-271b7fe4efde> in <module> 3----> 1 data['Sex'].replace(['male','female'], [0, 1], inplace=True) 4 5/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in replace(self, to_replace, value, inplace, limit, regex, method) 6 4365 limit=limit, 7 4366 regex=regex, 8-> 4367 method=method, 9 4368 ) 10 4369 11 12/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method) 13 6734 dest_list=value, 14 6735 inplace=inplace, 15-> 6736 regex=regex, 16 6737 ) 17 6738 18 19/opt/conda/lib/python3.6/site-packages/pandas/core/internals/managers.py in replace_list(self, src_list, dest_list, inplace, regex) 20 610 return _compare_or_regex_search(values, s, regex) 21 611 22--> 612 masks = [comp(s, regex) for i, s in enumerate(src_list)] 23 613 24 614 result_blocks = [] 25 26/opt/conda/lib/python3.6/site-packages/pandas/core/internals/managers.py in <listcomp>(.0) 27 610 return _compare_or_regex_search(values, s, regex) 28 611 29--> 612 masks = [comp(s, regex) for i, s in enumerate(src_list)] 30 613 31 614 result_blocks = [] 32 33/opt/conda/lib/python3.6/site-packages/pandas/core/internals/managers.py in comp(s, regex) 34 608 maybe_convert_objects(values), s.asm8, regex 35 609 ) 36--> 610 return _compare_or_regex_search(values, s, regex) 37 611 38 612 masks = [comp(s, regex) for i, s in enumerate(src_list)] 39 40/opt/conda/lib/python3.6/site-packages/pandas/core/internals/managers.py in _compare_or_regex_search(a, b, regex) 41 1965 raise TypeError( 42 1966 "Cannot compare types {a!r} and {b!r}".format( 43-> 1967 a=type_names[0], b=type_names[1] 44 1968 ) 45 1969 ) 46 47TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str' 48 49### 該当のソースコード 50 51```python 52import numpy as np 53import pandas as pd 54!ls ../input/titanic 55 56train = pd.read_csv('../input/titanic/train.csv') 57test = pd.read_csv('../input/titanic/test.csv') 58gender_submission = pd.read_csv('../input/titanic/gender_submission.csv') 59 60gender_submission.head() 61 62train.tail() 63 64test.tail() 65 66data = pd.concat([train,test], sort=False) 67 68data.tail() 69 70print(len(train), len(test), len(data)) 71 72data.isnull().sum() 73 74data['Sex'].replace(['male','female'], [0, 1], inplace=True)
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
KaggleのNotebook上で実行しています。
よろしくお願い致します
あなたの回答
tips
プレビュー