通过从右表中采样来填充左连接的 NaN 值

我无法找到一种很好的熊猫式方法来通过从右表中采样来填充左连接缺失的 NaN 值。


例如joined_left = left.merge(right, how="left", left_on=[attr1], right_on=[attr2]) 从左到右


   0  1  2

0  1  1  1

1  2  2  2

2  3  3  3

3  9  9  9

4  1  3  2


   0  1  2

0  1  2  2

1  1  2  3

2  3  2  2

3  3  2  9

4  3  2  2

产生像


   0  1_x  2_x  1_y  2_y

0  1    1    1  2.0  2.0

1  1    1    1  2.0  3.0

2  2    2    2  NaN  NaN

3  3    3    3  2.0  2.0

4  3    3    3  2.0  9.0

5  3    3    3  2.0  2.0

6  9    9    9  NaN  NaN

7  1    3    2  2.0  2.0

8  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

基本上随机采样并使用 fillna() 首次出现 NaN 值来填充......但由于某种原因我没有得到任何输出。


谢谢!


输出之一可能是


   0  1_x  2_x  1_y  2_y

0  1    1    1  2.0  2.0

1  1    1    1  2.0  3.0

2  2    2    2  2.0  2.0

3  3    3    3  2.0  2.0

4  3    3    3  2.0  9.0

5  3    3    3  2.0  2.0

6  9    9    9  3.0  2.9

7  1    3    2  2.0  2.0

8  1    3    2  2.0  3.0

与采样3  2  2和3  2  9


元芳怎么了
浏览 139回答 1
1回答

智慧大石

使用sample与fillnajoined_left = left.merge(right, how="left", left_on=[0], right_on=[0],indicator=True) # adding indicatorjoined_leftOut[705]:    0  1_x  2_x  1_y  2_y     _merge0  1    1    1  2.0  2.0       both1  1    1    1  2.0  3.0       both2  2    2    2  NaN  NaN  left_only3  3    3    3  2.0  2.0       both4  3    3    3  2.0  9.0       both5  3    3    3  2.0  2.0       both6  9    9    9  NaN  NaN  left_only7  1    3    2  2.0  2.0       both8  1    3    2  2.0  3.0       bothnnull=joined_left['_merge'].eq('left_only').sum() # find all many row miss match , at the mergedfs=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     _merge0  1    1    1  2.0  2.0       both1  1    1    1  2.0  3.0       both2  2    2    2  2.0  2.0  left_only3  3    3    3  2.0  2.0       both4  3    3    3  2.0  9.0       both5  3    3    3  2.0  2.0       both6  9    9    9  2.0  3.0  left_only7  1    3    2  2.0  2.0       both8  1    3    2  2.0  3.0       both
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python