1

I have a pandas dataframe like this:

     df 
                  Case   Type   Base
        Year
        2000      HI      PC1   0
        2001      HI      PC1   0
        2003      HI      PC1   2.0
        2004      HI      PC1   0
        2005      HI      PC2   0
        2006      HI      PC2   0
        2007      HI      PC2   2.0
        2008      HI      PC2   0
        2009      LO      PC1   0
        2010      LO      PC1   0
        2011      LO      PC1   2.0
        2012      LO      PC1   0
        2013      LO      PC2   0
        2014      LO      PC2   0
        2015      LO      PC2   2
        2016      LO      PC2   0

I want to replace some of the zero values not all of them in column ['Base'] with value 1, so the final df looks like this:

          df 
                    Case   Type   Base
         Year
         2000      HI      PC1   1
         2001      HI      PC1   1
         2003      HI      PC1   2.0
         2004      HI      PC1   0
         2005      HI      PC2   1
         2006      HI      PC2   1
         2007      HI      PC2   2.0
         2008      HI      PC2   0
         2009      LO      PC1   1
         2010      LO      PC1   1
         2011      LO      PC1   2.0
         2012      LO      PC1   0
         2013      LO      PC2   1
         2014      LO      PC2   1
         2015      LO      PC2   2.0
         2016      LO      PC2   0

I used the code below:

         df.groubpy(['Case','Type'].apply(lambda x: x.[0:3,3].replace({0:1})

But I realize I only get the results between 2000 and 2001, not between 2000,2016.

Thank you for your help.

1
  • so which zeros specifically do u want to replace? what's the condition? Commented Feb 27, 2020 at 22:51

1 Answer 1

2

mask and bfill

zeroes = df.Base.eq(0)
shiftd = zeroes.astype(int).shift().bfill()
df.assign(Base=df.Base.mask(zeroes).fillna(shiftd))

     Case Type  Base
Year                
2000   HI  PC1   1.0
2001   HI  PC1   1.0
2003   HI  PC1   2.0
2004   HI  PC1   0.0
2005   HI  PC2   1.0
2006   HI  PC2   1.0
2007   HI  PC2   2.0
2008   HI  PC2   0.0
2009   LO  PC1   1.0
2010   LO  PC1   1.0
2011   LO  PC1   2.0
2012   LO  PC1   0.0
2013   LO  PC2   1.0
2014   LO  PC2   1.0
2015   LO  PC2   2.0
2016   LO  PC2   0.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help. Worked.

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.