Reputation: 1
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?
Upvotes: 0
Views: 62
Reputation: 1153
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
Upvotes: 1