Pandas dataframe change all value if greater than 0 [duplicate]
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
pandas dataframe
marked as duplicate by jpp
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.
add a comment |
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
pandas dataframe
marked as duplicate by jpp
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 afterdf.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 thendf.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
add a comment |
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
pandas dataframe
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
pandas dataframe
asked Nov 24 '18 at 14:14
fred.schwartzfred.schwartz
3118
3118
marked as duplicate by jpp
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
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 afterdf.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 thendf.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
add a comment |
1
Are you afterdf.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 thendf.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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
You can do this:
df.clip_upper(1)
or this:
df.where(df < 1, other=1)
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
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Nov 24 '18 at 14:45
answered Nov 24 '18 at 14:19
jezraeljezrael
326k23268344
326k23268344
add a comment |
add a comment |
You can do this:
df.clip_upper(1)
or this:
df.where(df < 1, other=1)
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
|
show 1 more comment
You can do this:
df.clip_upper(1)
or this:
df.where(df < 1, other=1)
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
|
show 1 more comment
You can do this:
df.clip_upper(1)
or this:
df.where(df < 1, other=1)
You can do this:
df.clip_upper(1)
or this:
df.where(df < 1, other=1)
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
|
show 1 more comment
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
|
show 1 more comment
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