第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

np.where 與列表元素選擇

np.where 與列表元素選擇

慕村225694 2023-09-05 20:19:54
我有一個如下所示的數(shù)據(jù)樣本(真實(shí)數(shù)據(jù)集有更多列):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]]僅當(dāng) IDct 數(shù)字大于 1 時,我才想獲取 Index1 和 Index2 列(列表)中的第二個元素(因?yàn)?IDct 數(shù)字表示列表中有多少個元素)。我正在嘗試以下答案,但都出現(xiàn)“列表索引超出范圍”的錯誤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)我應(yīng)該采取什么不同的做法?謝謝!
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超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])


查看完整回答
反對 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號