2017-09-12 68 views
1

我想應用一個數據幀的特定列向後填充滿足以下條件:我有「colum_A」可以假設只有四個值,稱爲A,B,C,D,向後填充應該工作,如以下:向後填充條件

if the first not NaN is A, then backward_filling with A; 

if the first not NaN is B, then backward_filling with B; 

if the first not NaN is C, then backward_filling with B; 

if the first not NaN is D, then backward_filling with C; 

if the column_A only contains NaN, then backward_filling with D 

例如:

輸入DF:

colum_A 
NaN 
NaN 
B 
B 
C 
C 

輸出DF:

colum_A 
B 
B 
C 
C 
D 
D 

請任何幫助,將不勝感激。 最好的問候, 卡羅

回答

1

我覺得你條件需要mapbfill

#get mask for back filling NaNs 
m = df['colum_A'].isnull() 
d = {'A':'A','B':'B','C':'B','D':'C'} 
#D if all values NaN 
df['colum_B'] = 'D' if m.all() else np.where(m, df['colum_A'].map(d).bfill(),df['colum_A']) 
#alternative 
#df['colum_B'] = 'D' if m.all() else df['colum_A'].mask(m, df['colum_A'].map(d).bfill()) 
print (df) 
    colum_A colum_B 
0  NaN  B 
1  NaN  B 
2  B  B 
3  A  A 
4  NaN  B 
5  C  C 
6  C  C 
7  NaN  C 
8  NaN  C 
9  NaN  C 
10  D  D 
11  D  D 
12  A  A 
13  C  C 
14  NaN  A 
15  A  A 
16  NaN  NaN 
+0

是。有效。許多感謝Jezrael。請問,你能否提供一些關於np.where在這種特殊情況下的更多信息? –

+0

當然,函數['numpy.where'](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html)像其他數組一樣工作。我想鏈接希望更多。 – jezrael

+0

太棒了。非常感謝你。非常感激。 –