欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pandas对指定列进行填充的方法

程序员文章站 2023-12-01 20:36:40
实例如下所示: >>> import pandas as pd >>> import numpy as np >&g...

实例如下所示:

>>> import pandas as pd
>>> import numpy as np
>>> ts1 = [0, 1, np.nan, np.nan, np.nan, np.nan]
>>> ts2 = [0, 2, np.nan, 3, np.nan, np.nan]
>>> d = {'X': ts1, 'Y': ts2, 'Z': ts2}
>>> df = pd.DataFrame(data=d)
>>> df
   X  Y  Z
0 0.0 0.0 0.0
1 1.0 2.0 2.0
2 NaN NaN NaN
3 NaN 3.0 3.0
4 NaN NaN NaN
5 NaN NaN NaN
>>> col =['X','Y']
>>> df[col] = df[col].ffill()
>>> df
   X  Y  Z
0 0.0 0.0 0.0
1 1.0 2.0 2.0
2 1.0 2.0 NaN
3 1.0 3.0 3.0
4 1.0 3.0 NaN
5 1.0 3.0 NaN
>>> 

以上这篇pandas对指定列进行填充的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。