Pandas在

时间:2017-06-14 07:21:07

标签: pandas replace

我有以下数据框:

df = pd.DataFrame({'A' : [1., 2., 3., 4.], 'B' : [4., 3., 2., 1.], 'C' : [pd.NaT,8,pd.NaT,0]})
df

enter image description here

每当C与NaT不同时,我想将B列更改为C的值。最终结果如下:

enter image description here

2 个答案:

答案 0 :(得分:3)

使用combine_first

df['B'] = df['C'].combine_first(df['B'])
print (df)
     A  B    C
0  1.0  4  NaT
1  2.0  8    8
2  3.0  2  NaT
3  4.0  0    0

如果需要float s:

df['B'] = df['C'].combine_first(df['B']).astype(df['B'].dtype)
print (df)
     A    B    C
0  1.0  4.0  NaT
1  2.0  8.0    8
2  3.0  2.0  NaT
3  4.0  0.0    0

masknotnull

df['B'] = df['B'].mask(df['C'].notnull(), df['C'])
print (df)
     A  B    C
0  1.0  4  NaT
1  2.0  8    8
2  3.0  2  NaT
3  4.0  0    0

numpy.where

df['B'] = np.where(df['C'].notnull(), df['C'], df['B'])
print (df)
     A  B    C
0  1.0  4  NaT
1  2.0  8    8
2  3.0  2  NaT
3  4.0  0    0

答案 1 :(得分:2)

pd.Series.fillna

df.B = df.C.fillna(df.B)
df

     A    B    C
0  1.0  4.0  NaT
1  2.0  8.0    8
2  3.0  2.0  NaT
3  4.0  0.0    0

pd.DataFrame.where

df.B = df.B.where(df.C.isnull(), df.C).astype(df.B.dtype)
df

     A    B    C
0  1.0  4.0  NaT
1  2.0  8.0    8
2  3.0  2.0  NaT
3  4.0  0.0    0