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

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

使用 if else 在 Jupyter 中創建海運圖時出錯

使用 if else 在 Jupyter 中創建海運圖時出錯

一只斗牛犬 2023-06-20 16:17:23
我試圖用 seaborn 在 python 中為日期列創建一個圖表。我收到以下錯誤消息。你知道如何解決它嗎?----> 4 如果 df['MS_Date']=="s.xii" 或 df['MS_Date']=="s.xii(1)" 或 df['MS_Date']=="s.xii (2)" 或 df['MS_Date']=="s.xii(in)" 或 df['MS_Date']=="s.xii(ex)" 或 df['MS_Date']=="s. xii(med)": 5 df['MS_Date']== "12th century" 6 如果 df['MS_Date']=="s.xii/xiii" 或 df['MS_Date']=="s.xii/十三”:~\anaconda\lib\site-packages\pandas\core\generic.py in nonzero (self) 1477 1478 模棱兩可的,我們的意思是它匹配輸入的水平 -> 1479 axis和另一個軸的標簽。1480 1481 參數ValueError:Series 的真值不明確。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。代碼:if df['MS_Date']=="s.xii" or df['MS_Date']=="s.xii(1)" or df['MS_Date']=="s.xii(2)" or df['MS_Date']=="s.xii(in)" or df['MS_Date']=="s.xii(ex)" or df['MS_Date']=="s.xii(med)":         df['MS_Date']== "12th century"if df['MS_Date']=="s.xii/xiii" or df['MS_Date']=="s.xii/xiii":    df['MS_Date']=="12 & 13 century"if df['MS_Date']=="s.xiii" or df['MS_Date']=="s.xiii(1)" or df['MS_Date']=="s.xiii(2)" or df['MS_Date']=="s.xiii(in)" or df['MS_Date']=="s.xiii(ex)" or df['MS_Date']=="s.xiii(med)":    df['MS_Date']=="13 century"if df['MS_Date']=="s.xiii/s.xiv" or df['MS_Date']=="s.xiii/xiv":    df['MS_Date']=="13 & 14 century"if df['MS_Date']=="s.xiii/s.xv":    df['MS_Date']=="13 & 15 century"if  df['MS_Date']=="s.xiv" or df['MS_Date']=="s.xiv(1)" or df['MS_Date']=="s.xiv(2)" or df['MS_Date']=="s.xiv(in)" or df['MS_Date']=="s.xiv(ex)"  or df['MS_Date']=="s.xiv(med)":     df['MS_Date']=="14th century"if  df['MS_Date']=="s.xiv/xv":    df['MS_Date']== "14 & 15 century"if df['MS_Date']=="s.xv" or df['MS_Date']=="s.xv(1)" or df['MS_Date']=="s.xv(2)" or df['MS_Date']=="s.xv(in)" or df['MS_Date']=="s.xv(ex)" or df['MS_Date']=="s.xv(med)" :    df['MS_Date']=="15th century"if  df['MS_Date']=="s.xv/xvi":    df['MS_Date']== "15 & 16 century"if df['MS_Date']=="xvi" or df['MS_Date']=="s.xvi(in)":    df['MS_Date']== "16 century"else:    df['MS_Date']=="unknown"
查看完整描述

1 回答

?
白豬掌柜的

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

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

此錯誤意味著您正在獲取一系列布爾值而不是單個布爾值。對于使用 if 和 alse 你需要一個單一的True/False值,但在這里通過使用你會得到一個系列,它會根據它們滿足的標準df['MS_Date']=="s.xii"為你提供所有位置。True/False


樣品 df:


    Player      Position    color

0   Pele        Forward     black

1   Platini     Midfielder  white

2   Beckenbauer Defender    red

如果我想根據某些條件更改顏色列的值,那么我將這樣寫而不是使用 if 和 else:


df.loc[(df['Player']=='Pele') | (df['Position']=='Forward'), 'color'] = 'white'

或者


你可以這樣寫numpy.where:


np.where(<condition>, <value1 if true>, <value2 if false>) 

df['color'] = np.where((df['Player']=='Pele') | (df['Position']=='Forward'), 'black', df.color)

為了使其簡潔明了,np.select請在此處查看如何使用: https://stackoverflow.com/a/60244752/6660373


也==可以使用而不是使用eq


我將使用 loc 來獲取我的條件成立的位置。在那個位置上,我將選擇一個要更改的列。df.loc[<conditions>, <column to assign>] = <value>


像這樣使用:


df.loc[(df['MS_Date']=="s.xii") | (df['MS_Date']=="s.xii(1)") | (df['MS_Date']=="s.xii(2)") | (df['MS_Date']=="s.xii(in)") | (df['MS_Date']=="s.xii(ex)") | (df['MS_Date']=="s.xii(med)"), 'MS_Date']= "12th century"

df.loc[(df['MS_Date']=="s.xii/xiii") | (df['MS_Date']=="s.xii/xiii"), 'MS_Date']="12 & 13 century"



查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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