2

I have a dataframe like this [![Dataframe looks like this][1]: https://i.stack.imgur.com/R7GmM.png Now I want to skip nan's and so that data shift towards left i.e. [![formatted dataframe should be like this] [1]: https://i.stack.imgur.com/yGYIy.png

I am not able to do it via pandas. Is a scalable solution possible for this if I have a large dataset of let say 100k rows?

[EDIT]: Here is the data and desired output:

#Original df
>>> df
   A    B    C    D
0  a  NaN    c  NaN
1  b  NaN    b    a
2  c  NaN  NaN    d
3  d    a    b    c

#Desired output:

   A  B  C  D
0  a  c      
1  b  b  a   
2  c  d      
3  d  a  b  c
sacuL
  • 49,704
  • 8
  • 81
  • 106
nOObda
  • 123
  • 1
  • 2
  • 9
  • 2
    Please re-work your question to include an example. It looks as though you tried to put images in your post, but they didn't show up. In any case, cutting and pasting example data into the body of your question to produce a [MCVE] is *always* better than posting an image of your dataframe. – sacuL Apr 12 '18 at 11:58
  • I have edited the links. Kindly check the links for the problem. – nOObda Apr 12 '18 at 12:01
  • Note I have edited your question to include an example. In the future, please try to post examples in a similar way, working with screenshots of dataframes is not easy and won't attract many people to answer your question. – sacuL Apr 12 '18 at 12:09

1 Answers1

3

Here is one method:

Starting from your dataframe named df:

   A    B    C    D
0  a  NaN    c  NaN
1  b  NaN    b    a
2  c  NaN  NaN    d
3  d    a    b    c

apply these line:

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

And you get your resulting shifted_df dataframe:

>>> shifted_df
   A  B  C  D
0  a  c      
1  b  b  a   
2  c  d      
3  d  a  b  c
sacuL
  • 49,704
  • 8
  • 81
  • 106