0

I have à dataframe with millions of row. As example of my dataframe :

Col0, Col1, Col2, Col3
Val0, Val1, Nan, Nan
Val0, Nan, Val3, Nan
Val0, Nan, Nan, Val5

My desired output is :

Col0, Col1, Col2, Col3
Val0, Val1, Val3, Val5

I make an iteration request for the desired output but this take hours.

Regards

0

1 Answer 1

1

It is unclear to me what the advantage of doing this would be unless your df simply is a list in which there is no realtion between columns, but here is a solution to this:

for this df:

 Col0  Col1  Col2  Col3
0     3     5   Nan   Nan
1     3   Nan    12   Nan
2     3   Nan   Nan    17

do this

pd.concat([testcol[col].sort_values().reset_index(drop=True) for col in testcol], axis=1, ignore_index=True)

which gives

   0     1     2     3
0  3     5    12    17
1  3   Nan   Nan   Nan
2  3   Nan   Nan   Nan
Sign up to request clarification or add additional context in comments.

2 Comments

If you drop all but the first row this is the answer. Though pretty pointless using a DataFrame for that, as you rightly pointed out
Yes. I thought the nan row drop could be left to the asker as it doesn't demand any effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.