亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將兩個數據幀與 pd 合并。合并列中的 NA 產生“類型錯誤:NA 的布爾值不明確”

將兩個數據幀與 pd 合并。合并列中的 NA 產生“類型錯誤:NA 的布爾值不明確”

蝴蝶不菲 2022-08-02 17:36:16
在 Pandas 1.0.1 中,如果df = df.merge(df2, on=some_column)收益 率File /home/torstein/code/fintechdb/Sheets/sheets/gild.py, line 42, in gild    df = df.merge(df2, on=some_column)File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py, line 7297, in merge    validate=validate,File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 88, in merge    return op.get_result()File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 643, in get_result    join_index, left_indexer, right_indexer = self._get_join_info()File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 862, in _get_join_info    (left_indexer, right_indexer) = self._get_join_indexers()File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 841, in _get_join_indexers    self.left_join_keys, self.right_join_keys, sort=self.sort, how=self.howFile /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1311, in _get_join_indexers    zipped = zip(*mapped)File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1309, in <genexpr>    for n in range(len(left_keys))File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1918, in _factorize_keys    rlab = rizer.factorize(rk)File pandas/_libs/hashtable.pyx, line 77, in pandas._libs.hashtable.Factorizer.factorizeFile pandas/_libs/hashtable_class_helper.pxi, line 1817, in pandas._libs.hashtable.PyObjectHashTable.get_labelsFile pandas/_libs/hashtable_class_helper.pxi, line 1732, in pandas._libs.hashtable.PyObjectHashTable._uniqueFile pandas/_libs/missing.pyx, line 360, in pandas._libs.missing.NAType.__bool__TypeError: boolean value of NA is ambiguous
查看完整描述

2 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

這與在 pandas 1.0.0 中實現以及 pandas 團隊如何決定它應該在布爾上下文中工作有關。此外,考慮到它是一個實驗性功能,因此除了實驗之外,它不應該用于任何事情:pd.NA

警告實驗:pd的行為。NA 仍然可以在沒有警告的情況下更改。

在pandas文檔的另一個鏈接中,它涵蓋了使用缺失值的工作,我相信可以找到您正在尋找的原因和答案:

布爾上下文中的 NA:由于 NA 的實際值是未知的,因此將 NA 轉換為布爾值是不明確的。下面會引發錯誤:TypeError:NA 的布爾值不明確

此外,它還提供了一個有價值的建議:

“這也意味著pd。NA 不能在計算為布爾值的上下文中使用,例如 if 條件:...其中條件可能是pd。那。在這種情況下,isna() 可用于檢查 pd。NA 或條件為 pd。可以避免NA,例如通過事先填充缺失值。


查看完整回答
反對 回復 2022-08-02
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

我決定我的數據中的實例是有效的,因此我需要處理它們而不是填充它們,就像.如果您像我一樣在這種情況下,請將其從轉換為其中之一或簡單地使用.只有您可以決定 null 是應該出來 T 還是 F,但這里有一個簡單的例子:pd.NAfillna()pd.NATrueFalsepd.isna(val)


val = pd.NA

if pd.isna(val) :

    print('it is null')

else :

    print('it is not null')

返回:it is null


然后


val = 7

if pd.isna(val) :

    print('it is null')

else :

    print('it is not null')

返回:it is not null


希望這有助于其他人獲得明確的行動方案(Celius的答案是準確的,但我想為那些為此而苦苦掙扎的人提供可操作的代碼)。


查看完整回答
反對 回復 2022-08-02
  • 2 回答
  • 0 關注
  • 295 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號