将NaN移至其各自行的末尾 [英] Shift NaNs to the end of their respective rows
本文介绍了将NaN移至其各自行的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataFrame:
I have a DataFrame like :
0 1 2
0 0.0 1.0 2.0
1 NaN 1.0 2.0
2 NaN NaN 2.0
我想得到的是
What I want to get is
Out[116]:
0 1 2
0 0.0 1.0 2.0
1 1.0 2.0 NaN
2 2.0 NaN NaN
这是我目前的做法.
This is my approach as of now.
df.apply(lambda x : (x[x.notnull()].values.tolist()+x[x.isnull()].values.tolist()),1)
Out[117]:
0 1 2
0 0.0 1.0 2.0
1 1.0 2.0 NaN
2 2.0 NaN NaN
有没有有效的方法来实现这一目标? apply
这是减慢速度的方法.
谢谢您的助手!:)
Is there any efficient way to achieve this ? apply
Here is way to slow .
Thank you for your assistant!:)
我的真实数据大小
df.shape
Out[117]: (54812040, 1522)
推荐答案
以下是使用 justify
-
Here's a NumPy solution using justify
-
In [455]: df
Out[455]:
0 1 2
0 0.0 1.0 2.0
1 NaN 1.0 2.0
2 NaN NaN 2.0
In [456]: pd.DataFrame(justify(df.values, invalid_val=np.nan, axis=1, side='left'))
Out[456]:
0 1 2
0 0.0 1.0 2.0
1 1.0 2.0 NaN
2 2.0 NaN NaN
如果要保存内存,请改回分配-
If you want to save memory, assign it back instead -
df[:] = justify(df.values, invalid_val=np.nan, axis=1, side='left')
这篇关于将NaN移至其各自行的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文