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

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

通過從右表中采樣來填充左連接的 NaN 值

通過從右表中采樣來填充左連接的 NaN 值

元芳怎么了 2021-08-14 17:01:10
我無法找到一種很好的熊貓式方法來通過從右表中采樣來填充左連接缺失的 NaN 值。例如joined_left = left.merge(right, how="left", left_on=[attr1], right_on=[attr2]) 從左到右   0  1  20  1  1  11  2  2  22  3  3  33  9  9  94  1  3  2   0  1  20  1  2  21  1  2  32  3  2  23  3  2  94  3  2  2產(chǎn)生像   0  1_x  2_x  1_y  2_y0  1    1    1  2.0  2.01  1    1    1  2.0  3.02  2    2    2  NaN  NaN3  3    3    3  2.0  2.04  3    3    3  2.0  9.05  3    3    3  2.0  2.06  9    9    9  NaN  NaN7  1    3    2  2.0  2.08  1    3    2  2.0  3.0如何從右表中采樣一行而不是填充 NaN?這是我到目前為止嘗試過的操場:left = [[1,1,1], [2,2,2],[3,3,3], [9,9,9], [1,3,2]]right = [[1,2,2],[1,2,3],[3,2,2], [3,2,9], [3,2,2]]left = np.asarray(left)right = np.asarray(right)left = pd.DataFrame(left)right = pd.DataFrame(right)joined_left = left.merge(right, how="left", left_on=[0], right_on=[0])while(joined_left.isnull().values.any()):    right_sample = right.sample().drop(0, axis=1)    joined_left.fillna(value=right_sample, limit=1)print joined_left基本上隨機(jī)采樣并使用 fillna() 首次出現(xiàn) NaN 值來填充......但由于某種原因我沒有得到任何輸出。謝謝!輸出之一可能是   0  1_x  2_x  1_y  2_y0  1    1    1  2.0  2.01  1    1    1  2.0  3.02  2    2    2  2.0  2.03  3    3    3  2.0  2.04  3    3    3  2.0  9.05  3    3    3  2.0  2.06  9    9    9  3.0  2.97  1    3    2  2.0  2.08  1    3    2  2.0  3.0與采樣3  2  2和3  2  9
查看完整描述

1 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊

使用sample與fillna


joined_left = left.merge(right, how="left", left_on=[0], right_on=[0],indicator=True) # adding indicator

joined_left

Out[705]: 

   0  1_x  2_x  1_y  2_y     _merge

0  1    1    1  2.0  2.0       both

1  1    1    1  2.0  3.0       both

2  2    2    2  NaN  NaN  left_only

3  3    3    3  2.0  2.0       both

4  3    3    3  2.0  9.0       both

5  3    3    3  2.0  2.0       both

6  9    9    9  NaN  NaN  left_only

7  1    3    2  2.0  2.0       both

8  1    3    2  2.0  3.0       both

nnull=joined_left['_merge'].eq('left_only').sum() # find all many row miss match , at the mergedf

s=right.sample(nnull)# rasmple from the dataframe after dropna 

s.index=joined_left.index[joined_left['_merge'].eq('left_only')] # reset the index of the subset fill df to the index of null value show up 

joined_left.fillna(s.rename(columns={1:'1_y',2:'2_y'})) 

Out[706]: 

   0  1_x  2_x  1_y  2_y     _merge

0  1    1    1  2.0  2.0       both

1  1    1    1  2.0  3.0       both

2  2    2    2  2.0  2.0  left_only

3  3    3    3  2.0  2.0       both

4  3    3    3  2.0  9.0       both

5  3    3    3  2.0  2.0       both

6  9    9    9  2.0  3.0  left_only

7  1    3    2  2.0  2.0       both

8  1    3    2  2.0  3.0       both


查看完整回答
反對 回復(fù) 2021-08-14
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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