0

I have a pandas dataframe that currently looks like this

|Eriksson| NaN    | Boeser | NaN   |
| NaN    | McDavid| NaN    | NaN   |
| ...    | ...    | ...    | ...   |

I don't care whether its converted to a Numpy array or it remains a Data Frame, but I want an output object where the rows just consist of the non NaN values like this:

|Eriksson| Boeser|
|McDavid | NaN   |

(NaN because of the mismatched dimensions.) Is there any way to do this?

1
  • Could you add some more rows to the input / expected output so the pattern is clearer? Commented Mar 11, 2021 at 3:22

1 Answer 1

1

I think that this would do the trick for you:

df.apply(lambda x: pd.Series(x.dropna().values), axis=1)

Example:

>>> df = pd.DataFrame(np.random.randn(5,4))
>>> df.iloc[1,2] = np.NaN
>>> df.iloc[0,1] = np.NaN
>>> df.iloc[2,1] = np.NaN
>>> df.iloc[2,0] = np.NaN
>>> df
          0         1         2         3
0 -0.162388       NaN -0.299892  0.594846
1  3.165631 -1.190102       NaN -1.234934
2       NaN       NaN  0.885439 -1.714365
3 -1.622833 -1.319395 -1.716550 -0.517699
4  0.688479  0.576763  0.645344  0.708909
>>> df.apply(lambda x: pd.Series(x.dropna().values), axis=1)
          0         1         2         3
0 -0.162388 -0.299892  0.594846       NaN
1  3.165631 -1.190102 -1.234934       NaN
2  0.885439 -1.714365       NaN       NaN
3 -1.622833 -1.319395 -1.716550 -0.517699
4  0.688479  0.576763  0.645344  0.708909
Sign up to request clarification or add additional context in comments.

Comments

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.