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

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

np.where 與列表元素選擇

np.where 與列表元素選擇

慕村225694 2023-09-05 20:19:54
我有一個如下所示的數據樣本(真實數據集有更多列):data = {'stringID':['AB CD Efdadasfd','RFDS EDSfdsadf dsa','FDSADFDSADFFDSA'],'IDct':[1,2,3]} data = pd.DataFrame(data) data['Index1'] = [[3],[7,9],[5,6,8]] data['Index2'] = [[4],[10,13],[8,9,10]]僅當 IDct 數字大于 1 時,我才想獲取 Index1 和 Index2 列(列表)中的第二個元素(因為 IDct 數字表示列表中有多少個元素)。我正在嘗試以下答案,但都出現“列表索引超出范圍”的錯誤data['pos'] = np.where(data['IDct']>1, data.Index1.map(lambda x: x[1]),0) data['pos1']= np.where(data['IDct']>1, data.Index2.map(lambda x: x[1]),0)或者data['pos'] = np.where(data['IDct']>1, [x[1] for x in data['Index1']],0) data['pos1']= np.where(data['IDct']>1, [x[1] for x in data['Index2']],0)我應該采取什么不同的做法?謝謝!
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

您可以嘗試使用 loc 來分配列。

data.loc[data['IDct'] > 1, 'pos'] = data.loc[data['IDct'] > 1]['Index1'].apply(lambda x: x[1])
data.loc[data['IDct'] > 1, 'pos1'] = data.loc[data['IDct'] > 1]['Index2'].apply(lambda x: x[1])


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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