如何修复熊猫"np.where"中的不同类型的返回?



我有这个"np.其中";在脚本a中,但当它返回格式更改时,

我该怎么解决这个问题?我想在数据时间内返回。

In [22]: new[["DME","Ent. Prog."]]
Out[22]:
DME Ent. Prog.
0    2021-07-15
1    2021-07-15
...         ...        ...
7481 2021-07-13
7482 2021-07-13
[7483 rows x 2 columns]
In [23]: new["DME"] = np.where(new['Ent. Prog.'] == '', new["DME"],new['Ent. Prog.'])
In [24]: new[["DME","Ent. Prog."]]
Out[24]:
DME Ent. Prog.
0     1626307200000000000
1     1626307200000000000
...                   ...        ...
7481  1626134400000000000
7482  1626134400000000000
[7483 rows x 2 columns]

new["DME"] = np.where(new['Ent. Prog.'] == '', new["DME"],new['Ent. Prog.'])

原因是第二列没有按日期时间填充,可能的解决方案是转换这两列并测试misisng值:

new['DME'] = pd.to_datetime(new['DME'])
new['Ent. Prog.'] = pd.to_datetime(new['Ent. Prog.'])
new["DME"] = np.where(new['Ent. Prog.'].isna(), new["DME"],new['Ent. Prog.'])

或者用Series.fillna:中的另一列替换缺失的值

new["DME"] = new['Ent. Prog.'].fillna(new['DME'])

最新更新