我有以下DF
col1| col2
123| NaN
234| 234
456| NaN
567| 567
我想做的是,如果右边的单元格为空,则将左边的单元格复制到右边的单元格,因此输出为
col1| col2
123| 123
234| 234
456| 456
567| 567
我尝试过与fillna一起工作,但失败了
答案 0 :(得分:2)
按列使用ffill
,因此axis=1
:
df = df.ffill(axis=1)
print (df)
col1 col2
0 123.0 123.0
1 234.0 234.0
2 456.0 456.0
3 567.0 567.0
答案 1 :(得分:2)
conda update anaconda-navigator
答案 2 :(得分:1)
您可以使用np.where
软件包中的numpy
假设您的数据帧称为df
:
import numpy as np
df['col2'] = np.where(df['col2'].isnull(),df['col1'],df['col2'])
这将为您提供:
col1| col2
123| 123
234| 234
456| 456
567| 567