質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

2235閲覧

for文でリストの要素を回した際に出るエラーが何番目の要素に対してなのかを知りたい

onushinosenzo

総合スコア22

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/10/10 02:18

python

1sum_array = np.zeros((0,1280)) 2for i in range(len(result_)): 3 4 a=summary(result_[i], race_info_[i])[0] 5 sum_array = np.vstack((sum_array,a))

result_とrace_info_というリストには、それぞれ7869個のURLが要素として入っています。
この上記のコードを実行すると以下のようなエラーが出てしまいます。

TypeError Traceback (most recent call last) ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\nanops.py in f(values, axis, skipna, **kwds) 127 else: --> 128 result = alt(values, axis=axis, skipna=skipna, **kwds) 129 except Exception: ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\nanops.py in reduction(values, axis, skipna) 506 else: --> 507 result = getattr(values, meth)(axis) 508 ~\Anaconda3\envs\chainer\lib\site-packages\numpy\core\_methods.py in _amax(a, axis, out, keepdims, initial) 27 initial=_NoValue): ---> 28 return umr_maximum(a, axis, None, out, keepdims, initial) 29 TypeError: '>=' not supported between instances of 'str' and 'float' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-11-b187769d2892> in <module>() 7 for i in range(len(result_)): 8 ----> 9 a=summary(result_[i], race_info_[i])[0] 10 sum_array = np.vstack((sum_array,a)) <ipython-input-7-121d16024538> in summary(result_, race_info_) 35 df_['len'] = df_['len']/df_['len'].max() 36 df_['time'] = df_['time']/df_['time'].max() ---> 37 df_['gap'] = pd.Series(df_['gap'],dtype=float)/pd.Series(df_['gap'],dtype=float).max() 38 df_['rank'] = pd.Series(df_['rank'],dtype=float)/pd.Series(df_['rank'],dtype=float).max() 39 df_['cnt'] = pd.Series(df_['cnt'],dtype=float)/pd.Series(df_['cnt'],dtype=float).max() ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\generic.py in stat_func(self, axis, skipna, level, numeric_only, **kwargs) 9611 skipna=skipna) 9612 return self._reduce(f, name, axis=axis, skipna=skipna, -> 9613 numeric_only=numeric_only) 9614 9615 return set_function_name(stat_func, name, cls) ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\series.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds) 3219 'numeric_only.'.format(name)) 3220 with np.errstate(all='ignore'): -> 3221 return op(delegate, skipna=skipna, **kwds) 3222 3223 return delegate._reduce(op=op, name=name, axis=axis, skipna=skipna, ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\nanops.py in f(values, axis, skipna, **kwds) 129 except Exception: 130 try: --> 131 result = alt(values, axis=axis, skipna=skipna, **kwds) 132 except ValueError as e: 133 # we want to transform an object array ~\Anaconda3\envs\chainer\lib\site-packages\pandas\core\nanops.py in reduction(values, axis, skipna) 505 result = np.nan 506 else: --> 507 result = getattr(values, meth)(axis) 508 509 result = _wrap_results(result, dtype) ~\Anaconda3\envs\chainer\lib\site-packages\numpy\core\_methods.py in _amax(a, axis, out, keepdims, initial) 26 def _amax(a, axis=None, out=None, keepdims=False, 27 initial=_NoValue): ---> 28 return umr_maximum(a, axis, None, out, keepdims, initial) 29 30 def _amin(a, axis=None, out=None, keepdims=False, TypeError: '>=' not supported between instances of 'str' and 'float'

以下が関連しているsummary()です。

python

1def summary(result_, race_info_): 2 going_ = get_race_data(result_)[0] 3 wether_ = get_race_data(result_)[1] 4 len_ = get_race_data(result_)[2] 5 win = get_race_data(result_)[3] 6 7 link_lst = horse_page_link(race_info_) 8 9 df_ = pd.DataFrame() 10 11 #頭数足りない時用の0埋めデータフレーム作り 12 fill_z = np.zeros((10,8)) 13 zero_ = pd.DataFrame(fill_z) 14 zero_.rename(columns={0:'place',1:'len',2:'time',3:'gap',4:'wether',5:'going',6:'rank',7:'cnt'},inplace=True) 15 #レース数足りない時用の0埋めデータフレーム作り 16 fill_z_ = np.zeros((1,8)) 17 zero_row = pd.DataFrame(fill_z_) 18 zero_row.rename(columns={0:'place',1:'len',2:'time',3:'gap',4:'wether',5:'going',6:'rank',7:'cnt'},inplace=True) 19 20 #df_に全特徴量をまとめる 21 for i in range(len(link_lst)): 22 if len(uma_info(link_lst[i], going_, wether_, len_).index) < 10: 23 df_ = df_.append(uma_info(link_lst[i], going_, wether_, len_)) 24 for x in range(10 - len(uma_info(link_lst[i], going_, wether_, len_).index)): 25 df_ = df_.append(zero_row) 26 else: 27 df_ = df_.append(uma_info(link_lst[i], going_, wether_, len_)) 28 29 #16頭立てじゃないとき用の0埋め 30 for i in range(16-len(link_lst)): 31 df_ = df_.append(zero_) 32 33 34 #各インプット正規化 35 df_['len'] = df_['len']/df_['len'].max() 36 df_['time'] = df_['time']/df_['time'].max() 37 df_['gap'] = pd.Series(df_['gap'],dtype=float)/pd.Series(df_['gap'],dtype=float).max() 38 df_['rank'] = pd.Series(df_['rank'],dtype=float)/pd.Series(df_['rank'],dtype=float).max() 39 df_['cnt'] = pd.Series(df_['cnt'],dtype=float)/pd.Series(df_['cnt'],dtype=float).max() 40 41 #df_をflattenデータにする 42 data_summary = df_.round(5).values.flatten() 43 44 return data_summary ,win

df_['gap']に入る要素がエラーの原因であるのはわかるのですが、最初に記載したコード内のfor文の何回転目でエラーが起きているか知ることはできるのでしょうか?
それとも愚直にdf_['gap']内を探すしかないのでしょうか?
Jupyter Notebookを使用してます。
とりあえず一通り完成させたいという思いから、汚いコードになっています。申し訳ありません。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

今回はTypeErrorが起きているので、エラーが発生しそうなブロックをtry-catchでくくるといいかもしれません。

python

1items = [1, 2, 3, "4", 5] 2threshold = 3 3for i, item in enumerate(items): 4 try: 5 # エラーが起きそうな処理 6 isSmaller = item < threshold 7 print(isSmaller ) 8 except TypeError as e: 9 # エラーが起きた時の情報 10 print(e) 11 print(f"index : {i}, item : {item}")

投稿2018/10/10 02:34

tachikoma

総合スコア3601

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

i に何回転目か、ってのがはいってるので、
エラー出たときに i の値を出力するなり参照するなり。

投稿2018/10/10 02:27

y_waiwai

総合スコア87749

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

例外処理してみたらどうですか。

python

1sum_array = np.zeros((0,1280)) 2for i in range(len(result_)): 3 try: 4 a=summary(result_[i], race_info_[i])[0] 5 sum_array = np.vstack((sum_array,a)) 6 except TypeError: 7 # とりあえず見たい情報を見てプログラムを終了させることにする 8 print(i) 9 print(result_[i]) 10 print(race_info_[i]) 11 sys.exit() # sysをimportしておいてください

投稿2018/10/10 02:23

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

onushinosenzo

2018/10/10 03:57

ありがとうございます。無事解決することができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問