我有一个包含20000+值的Python DataFrame,如下所示。而且我希望有效地重新排列df,NaN会追逐一串价值。
IT1 IT2 IT3 IT4 IT5 IT6
0 qwe NaN NaN rew NaN NaN
1 NaN NaN sdc NaN NaN wer
2 NaN NaN NaN NaN NaN NaN
3 asd fsc ws zd ews df
.....
到
IT1 IT2 IT3 IT4 IT5 IT6
0 qwe rew NaN NaN NaN NaN
1 sdc wer NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN
3 asd fsc ws zd ews df
.....
所以每一行都没有像index = 2这样的值,或者像index = 3这样的所有值。有没有办法有效地重新排列我的数据帧df? 提前致谢
答案 0 :(得分:1)
一种方式,虽然速度很慢,但apply
,dropna
和tolist
:
df.apply(lambda x: pd.Series(x.dropna().tolist()),1)\
.set_axis(df.columns, axis=1, inplace=False)
输出:
IT1 IT2 IT3 IT4 IT5 IT6
0 qwe rew NaN NaN NaN NaN
1 sdc wer NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN
3 asd fsc ws zd ews df
答案 1 :(得分:1)
您可以编写一个自定义函数来对行进行排序,然后使用原始顺序中的列替换索引(列)。只需apply
它就行数据框
def row_sort(s):
s2 = s.sort_values()
s2.index = s.index
return s2
df.apply(row_sort, axis=1)
# returns:
IT1 IT2 IT3 IT4 IT5 IT6
0 qwe rew NaN NaN NaN NaN
1 sdc wer NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN
3 asd df ews fsc ws zd