Pandas dataframe change all value if greater than 0 [duplicate]












1
















This question already has an answer here:




  • How to replace negative numbers in Pandas Data Frame by zero

    4 answers




I have a dataframe



df=

A B C
1 2 55
0 44 0
0 0 0


and I want to change values to 1 if the value is >0.



Is this the right approach:
df.loc[df>0,]=1



to give: 
A B C
1 1 1
0 1 0
0 0 0









share|improve this question













marked as duplicate by jpp dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 1:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Are you after df.loc[df.gt(0).all(1), :] = 1 ?

    – Jon Clements
    Nov 24 '18 at 14:17











  • Just tried it and that didn't work but thanks for trying. Any other ideas?

    – fred.schwartz
    Nov 24 '18 at 14:18






  • 2





    well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

    – Jon Clements
    Nov 24 '18 at 14:19


















1
















This question already has an answer here:




  • How to replace negative numbers in Pandas Data Frame by zero

    4 answers




I have a dataframe



df=

A B C
1 2 55
0 44 0
0 0 0


and I want to change values to 1 if the value is >0.



Is this the right approach:
df.loc[df>0,]=1



to give: 
A B C
1 1 1
0 1 0
0 0 0









share|improve this question













marked as duplicate by jpp dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 1:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Are you after df.loc[df.gt(0).all(1), :] = 1 ?

    – Jon Clements
    Nov 24 '18 at 14:17











  • Just tried it and that didn't work but thanks for trying. Any other ideas?

    – fred.schwartz
    Nov 24 '18 at 14:18






  • 2





    well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

    – Jon Clements
    Nov 24 '18 at 14:19
















1












1








1









This question already has an answer here:




  • How to replace negative numbers in Pandas Data Frame by zero

    4 answers




I have a dataframe



df=

A B C
1 2 55
0 44 0
0 0 0


and I want to change values to 1 if the value is >0.



Is this the right approach:
df.loc[df>0,]=1



to give: 
A B C
1 1 1
0 1 0
0 0 0









share|improve this question















This question already has an answer here:




  • How to replace negative numbers in Pandas Data Frame by zero

    4 answers




I have a dataframe



df=

A B C
1 2 55
0 44 0
0 0 0


and I want to change values to 1 if the value is >0.



Is this the right approach:
df.loc[df>0,]=1



to give: 
A B C
1 1 1
0 1 0
0 0 0




This question already has an answer here:




  • How to replace negative numbers in Pandas Data Frame by zero

    4 answers








pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 14:14









fred.schwartzfred.schwartz

3118




3118




marked as duplicate by jpp dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 1:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by jpp dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 1:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Are you after df.loc[df.gt(0).all(1), :] = 1 ?

    – Jon Clements
    Nov 24 '18 at 14:17











  • Just tried it and that didn't work but thanks for trying. Any other ideas?

    – fred.schwartz
    Nov 24 '18 at 14:18






  • 2





    well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

    – Jon Clements
    Nov 24 '18 at 14:19
















  • 1





    Are you after df.loc[df.gt(0).all(1), :] = 1 ?

    – Jon Clements
    Nov 24 '18 at 14:17











  • Just tried it and that didn't work but thanks for trying. Any other ideas?

    – fred.schwartz
    Nov 24 '18 at 14:18






  • 2





    well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

    – Jon Clements
    Nov 24 '18 at 14:19










1




1





Are you after df.loc[df.gt(0).all(1), :] = 1 ?

– Jon Clements
Nov 24 '18 at 14:17





Are you after df.loc[df.gt(0).all(1), :] = 1 ?

– Jon Clements
Nov 24 '18 at 14:17













Just tried it and that didn't work but thanks for trying. Any other ideas?

– fred.schwartz
Nov 24 '18 at 14:18





Just tried it and that didn't work but thanks for trying. Any other ideas?

– fred.schwartz
Nov 24 '18 at 14:18




2




2





well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

– Jon Clements
Nov 24 '18 at 14:19







well, that's if all values are >0 for row, if you just want to max each value then df.clip_upper(1)? Your question title and body don't seem to be quite in tandem with what you might be asking.

– Jon Clements
Nov 24 '18 at 14:19














2 Answers
2






active

oldest

votes


















3














Use clip_upper:



df = df.clip_upper(1)
print (df)
A B C
0 1 1 1
1 0 1 0
2 0 0 0


Numpy alternative:



df = pd.DataFrame(np.clip(df.values, a_min=0, a_max=1), 
index=df.index,
columns=df.columns)
print (df)
A B C
0 1 1 1
1 0 1 0
2 0 0 0


And solution if no negative integer values - compare by ge (>=) and cast mask to integers:



print (df.ge(1))
A B C
0 True True True
1 False True False
2 False False False

df = df.ge(1).astype(int)
print (df)
A B C
0 1 1 1
1 0 1 0
2 0 0 0





share|improve this answer

































    1














    You can do this:



    df.clip_upper(1)


    or this:



    df.where(df < 1, other=1)





    share|improve this answer
























    • Yes to the first - but it's not equivalent to the second...

      – Jon Clements
      Nov 24 '18 at 14:22











    • This is great but there is a added complication that the value in question will not always be 0. It might be 20.

      – fred.schwartz
      Nov 24 '18 at 14:31











    • I've tried this df=np.where(df<20,df==0,df==1)

      – fred.schwartz
      Nov 24 '18 at 14:32













    • but that returns an array

      – fred.schwartz
      Nov 24 '18 at 14:32











    • df[df< 20] = 0 df[df!= 0] = 1

      – fred.schwartz
      Nov 24 '18 at 14:36


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Use clip_upper:



    df = df.clip_upper(1)
    print (df)
    A B C
    0 1 1 1
    1 0 1 0
    2 0 0 0


    Numpy alternative:



    df = pd.DataFrame(np.clip(df.values, a_min=0, a_max=1), 
    index=df.index,
    columns=df.columns)
    print (df)
    A B C
    0 1 1 1
    1 0 1 0
    2 0 0 0


    And solution if no negative integer values - compare by ge (>=) and cast mask to integers:



    print (df.ge(1))
    A B C
    0 True True True
    1 False True False
    2 False False False

    df = df.ge(1).astype(int)
    print (df)
    A B C
    0 1 1 1
    1 0 1 0
    2 0 0 0





    share|improve this answer






























      3














      Use clip_upper:



      df = df.clip_upper(1)
      print (df)
      A B C
      0 1 1 1
      1 0 1 0
      2 0 0 0


      Numpy alternative:



      df = pd.DataFrame(np.clip(df.values, a_min=0, a_max=1), 
      index=df.index,
      columns=df.columns)
      print (df)
      A B C
      0 1 1 1
      1 0 1 0
      2 0 0 0


      And solution if no negative integer values - compare by ge (>=) and cast mask to integers:



      print (df.ge(1))
      A B C
      0 True True True
      1 False True False
      2 False False False

      df = df.ge(1).astype(int)
      print (df)
      A B C
      0 1 1 1
      1 0 1 0
      2 0 0 0





      share|improve this answer




























        3












        3








        3







        Use clip_upper:



        df = df.clip_upper(1)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0


        Numpy alternative:



        df = pd.DataFrame(np.clip(df.values, a_min=0, a_max=1), 
        index=df.index,
        columns=df.columns)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0


        And solution if no negative integer values - compare by ge (>=) and cast mask to integers:



        print (df.ge(1))
        A B C
        0 True True True
        1 False True False
        2 False False False

        df = df.ge(1).astype(int)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0





        share|improve this answer















        Use clip_upper:



        df = df.clip_upper(1)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0


        Numpy alternative:



        df = pd.DataFrame(np.clip(df.values, a_min=0, a_max=1), 
        index=df.index,
        columns=df.columns)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0


        And solution if no negative integer values - compare by ge (>=) and cast mask to integers:



        print (df.ge(1))
        A B C
        0 True True True
        1 False True False
        2 False False False

        df = df.ge(1).astype(int)
        print (df)
        A B C
        0 1 1 1
        1 0 1 0
        2 0 0 0






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 14:45

























        answered Nov 24 '18 at 14:19









        jezraeljezrael

        326k23268344




        326k23268344

























            1














            You can do this:



            df.clip_upper(1)


            or this:



            df.where(df < 1, other=1)





            share|improve this answer
























            • Yes to the first - but it's not equivalent to the second...

              – Jon Clements
              Nov 24 '18 at 14:22











            • This is great but there is a added complication that the value in question will not always be 0. It might be 20.

              – fred.schwartz
              Nov 24 '18 at 14:31











            • I've tried this df=np.where(df<20,df==0,df==1)

              – fred.schwartz
              Nov 24 '18 at 14:32













            • but that returns an array

              – fred.schwartz
              Nov 24 '18 at 14:32











            • df[df< 20] = 0 df[df!= 0] = 1

              – fred.schwartz
              Nov 24 '18 at 14:36
















            1














            You can do this:



            df.clip_upper(1)


            or this:



            df.where(df < 1, other=1)





            share|improve this answer
























            • Yes to the first - but it's not equivalent to the second...

              – Jon Clements
              Nov 24 '18 at 14:22











            • This is great but there is a added complication that the value in question will not always be 0. It might be 20.

              – fred.schwartz
              Nov 24 '18 at 14:31











            • I've tried this df=np.where(df<20,df==0,df==1)

              – fred.schwartz
              Nov 24 '18 at 14:32













            • but that returns an array

              – fred.schwartz
              Nov 24 '18 at 14:32











            • df[df< 20] = 0 df[df!= 0] = 1

              – fred.schwartz
              Nov 24 '18 at 14:36














            1












            1








            1







            You can do this:



            df.clip_upper(1)


            or this:



            df.where(df < 1, other=1)





            share|improve this answer













            You can do this:



            df.clip_upper(1)


            or this:



            df.where(df < 1, other=1)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 14:20









            PiotrPiotr

            58946




            58946













            • Yes to the first - but it's not equivalent to the second...

              – Jon Clements
              Nov 24 '18 at 14:22











            • This is great but there is a added complication that the value in question will not always be 0. It might be 20.

              – fred.schwartz
              Nov 24 '18 at 14:31











            • I've tried this df=np.where(df<20,df==0,df==1)

              – fred.schwartz
              Nov 24 '18 at 14:32













            • but that returns an array

              – fred.schwartz
              Nov 24 '18 at 14:32











            • df[df< 20] = 0 df[df!= 0] = 1

              – fred.schwartz
              Nov 24 '18 at 14:36



















            • Yes to the first - but it's not equivalent to the second...

              – Jon Clements
              Nov 24 '18 at 14:22











            • This is great but there is a added complication that the value in question will not always be 0. It might be 20.

              – fred.schwartz
              Nov 24 '18 at 14:31











            • I've tried this df=np.where(df<20,df==0,df==1)

              – fred.schwartz
              Nov 24 '18 at 14:32













            • but that returns an array

              – fred.schwartz
              Nov 24 '18 at 14:32











            • df[df< 20] = 0 df[df!= 0] = 1

              – fred.schwartz
              Nov 24 '18 at 14:36

















            Yes to the first - but it's not equivalent to the second...

            – Jon Clements
            Nov 24 '18 at 14:22





            Yes to the first - but it's not equivalent to the second...

            – Jon Clements
            Nov 24 '18 at 14:22













            This is great but there is a added complication that the value in question will not always be 0. It might be 20.

            – fred.schwartz
            Nov 24 '18 at 14:31





            This is great but there is a added complication that the value in question will not always be 0. It might be 20.

            – fred.schwartz
            Nov 24 '18 at 14:31













            I've tried this df=np.where(df<20,df==0,df==1)

            – fred.schwartz
            Nov 24 '18 at 14:32







            I've tried this df=np.where(df<20,df==0,df==1)

            – fred.schwartz
            Nov 24 '18 at 14:32















            but that returns an array

            – fred.schwartz
            Nov 24 '18 at 14:32





            but that returns an array

            – fred.schwartz
            Nov 24 '18 at 14:32













            df[df< 20] = 0 df[df!= 0] = 1

            – fred.schwartz
            Nov 24 '18 at 14:36





            df[df< 20] = 0 df[df!= 0] = 1

            – fred.schwartz
            Nov 24 '18 at 14:36



            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)