这是一种非常麻烦的方法,首先用于
first_valid_index获取有效列,将返回的序列转换为数据框,以便我们可以
apply逐行调用并将其索引回原始df:
In [160]:def func(x): if x.values[0] is None: return None else: return df.loc[x.name, x.values[0]]pd.Dataframe(df.apply(lambda x: x.first_valid_index(), axis=1)).apply(func,axis=1)Out[160]:0 11 32 43 NaNdtype: float64
编辑
稍微干净一点的方法:
In [12]:def func(x): if x.first_valid_index() is None: return None else: return x[x.first_valid_index()]df.apply(func, axis=1)Out[12]:0 11 32 43 NaNdtype: float64
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)