这是我的数据框的示例:
HIP Ident
KOI-23 36130
KOI-321 27223
KOI-452 22329
NaN 74773
NaN 12831
我想做的是获取'Ident'列的值,并在它们为NaN时将其复制到'HIP'列,因此它们看起来像这样:
HIP Ident
KOI-23 36130
KOI-321 27223
KOI-452 22329
74773 74773
12831 12831
我已经尝试过使用np.where这样的方法,但这没用。
df['HIP']=np.where(df['HIP'].isnull(), df['HIP'], df['ident'])
那么,我该如何实现呢?
答案 0 :(得分:2)
您的情况与将isnull
更改为notnull
df['HIP'] = np.where(df['HIP'].notnull(), df['HIP'], df['Ident'])
答案 1 :(得分:1)
我的建议是使用.fillna
:
df['HIP'] = df['HIP'].fillna(df['Ident'])
df.head(10)
#output:
HIP Ident
0 KOI-23 36130
1 KOI-321 27223
2 KOI-452 22329
3 74773 74773
4 12831 12831