我很困惑,不知道發生了什么。我的腳本包含以下行。它用于將資料幀的三列的內容組合成其中之一(僅適用于滿足指定條件的行):
share_data_sm[yr]['MMR']= np.where((share_data_sm[yr]['MC']!='MA') & (share_data_sm[yr]['MC']!=' ') & (share_data_sm[yr]['MY']!=' '), share_data_sm[yr]['MC'].astype(str) share_data_sm[yr]['N'].astype(str) share_data_sm[yr]['MFR'].astype(str), share_data_sm[yr]['MFR'])
'share_data_sm' 是一個資料幀字典 - 每個“年”一個表。最讓我困惑的是,僅針對“yr”的一個特定值引發錯誤(命令是回圈的一部分,該回圈會遍歷多個“yr”值,除了這個特定值(2021)腳本運行流暢)。盡管 2021 資料框的資料內容可能存在一些特殊性,但沒有什么特別之處,一切都與其他資料完全一樣。以下是來自控制臺的回溯:
Traceback (most recent call last):
File "…ipykernel_1380/3858926177.py", line 1, in <module>
runfile('…work_folder/Groups/Structure/shareholding.py', wdir='…_work_folder/Groups/Structure')
File "…pydevd\_pydev_bundle\pydev_umd.py", line 167, in runfile
execfile(filename, namespace)
File "…pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents "\n", file, 'exec'), glob, loc)
File "…work_folder/Groups/Structure/shareholding.py", line 281, in <module>
share_data_sm[yr]['MMR']= np.where((share_data_sm[yr]['MC']!='MA') & (share_data_sm[yr]['MC']!=' ') & (share_data_sm[yr]['MY']!=' '), share_data_sm[yr]['MC'].astype(str) share_data_sm[yr]['N'].astype(str) share_data_sm[yr]['MFR'].astype(str), share_data_sm[yr]['MFR'])
File "…pandas\core\ops\common.py", line 69, in new_method
return method(self, other)
File "…pandas\core\arraylike.py", line 96, in __radd__
return self._arith_method(other, roperator.radd)
File "…pandas\core\frame.py", line 6864, in _arith_method
self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
File "…pandas\core\ops\__init__.py", line 306, in align_method_FRAME
left, right = left.align(
File "…pandas\core\frame.py", line 4677, in align
return super().align(
File "…pandas\core\generic.py", line 8591, in align
return self._align_series(
File "…pandas\core\generic.py", line 8708, in _align_series
join_index, lidx, ridx = join_index.join(
File "…pandas\core\indexes\base.py", line 207, in join
join_index, lidx, ridx = meth(self, other, how=how, level=level, sort=sort)
File "…pandas\core\indexes\base.py", line 3987, in join
return this.join(other, how=how, return_indexers=True)
File "…pandas\core\indexes\base.py", line 207, in join
join_index, lidx, ridx = meth(self, other, how=how, level=level, sort=sort)
File "…pandas\core\indexes\base.py", line 3995, in join
return self._join_monotonic(other, how=how)
File "…pandas\core\indexes\base.py", line 4327, in _join_monotonic
join_array, lidx, ridx = self._outer_indexer(other)
File "…pandas\core\indexes\base.py", line 345, in _outer_indexer
joined_ndarray, lidx, ridx = libjoin.outer_join_indexer(sv, ov)
File "…pandas\_libs\join.pyx", line 562, in pandas._libs.join.outer_join_indexer
TypeError: '<' not supported between instances of 'str' and 'int'
我將不勝感激 - 我該如何克服這個問題?
uj5u.com熱心網友回復:
我想我看到了。
代碼可以重新格式化為:
condition = \
(share_data_sm[yr]['MC']!='MA') & \
(share_data_sm[yr]['MC']!=' ') & \
(share_data_sm[yr]['MY']!=' ')
val_if_true = share_data_sm[yr]['MC'].astype(str) share_data_sm[yr]['N'].astype(str) share_data_sm[yr]['MFR'].astype(str)
val_if_false = share_data_sm[yr]['MFR']
share_data_sm[yr]['MMR'] = np.where(condition, val_if_true, val_if_false)
現在您可以看到 和 的值型別val_if_true不同val_if_false- 在第一種情況下,您將 3 個str值相加。在第二個中,您保留share_data_sm[yr]['MFR'].
我敢打賭,當您嘗試將兩種型別添加到同一個陣列中時,它會抱怨。
uj5u.com熱心網友回復:
回溯說錯誤是復雜的
np.where((share_data_sm[yr]['MC']!='MA') & (share_data_sm[yr]['MC']!=' ') & (share_data_sm[yr]['MY']!=' '), share_data_sm[yr]['MC'].astype(str) share_data_sm[yr]['N'].astype(str) share_data_sm[yr]['MFR'].astype(str), share_data_sm[yr]['MFR'])
但請記住,在將 3 個引數傳遞給where.
(share_data_sm[yr]['MC']!='MA') & (share_data_sm[yr]['MC']!=' ') & (share_data_sm[yr]['MY']!=' ')
share_data_sm[yr]['MC'].astype(str) share_data_sm[yr]['N'].astype(str) share_data_sm[yr]['MFR'].astype(str)
share_data_sm[yr]['MFR']
閱讀回溯有點困難,但str 錯誤在中間論點中暗示了這一點。但是您正在添加字串值。
但我看到了this.join,indices這表明它正在嘗試排列該系列的索引。所以幀索引可能主要是字串,帶有奇怪的數字索引。但這只是猜測;我不是pandas專家。
我建議先評估這 3 個引數,然后再將它們where用于更好地隔離問題。跨越多行的運算式很難除錯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416487.html
標籤:
上一篇:if(columnArow1=coloumnArow2,coulumnBrow2,"")excelif(logic_test,[value_if_true],[value_if_f
