2 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
首先,我建議您閱讀有關(guān)索引和在熊貓中選擇數(shù)據(jù)的內(nèi)容。Regaring可以使用的第一個(gè)問題.loc()與isnull()對(duì)列vaulues執(zhí)行布爾索引:
mask_nans = test.loc[3,:].isnull()
test.loc[3, mask_nans] = test.loc[0, mask_nans]
并且要將值加倍,您還可以使用以下方法直接乘以2切片數(shù)據(jù)幀.loc():
test.loc[1,'b':] *= 2
a b c d
0 1 4.0 7.0 7.0
1 2 10.0 16.0 16.0
2 3 6.0 9.0 9.0
3 4 4.0 7.0 7.0
4 5 NaN NaN NaN

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
用標(biāo)簽索引
如果您希望按 過濾a,并且a值是唯一的,請(qǐng)考慮將其設(shè)為索引以簡化您的邏輯并使其更高效:
test = test.set_index('a')
test.loc[4] = test.loc[4].fillna(test.loc[1])
test.loc[2] *= 2
布爾掩碼
如果a不是唯一的并且需要布爾掩碼,您仍然可以使用fillna附加步驟:
mask = test['a'].eq(4)
test.loc[mask] = test.loc[mask].fillna(test.loc[test['a'].eq(1).idxmax()])
test.loc[test['a'].eq(2)] *= 2
添加回答
舉報(bào)