How to replace numpy.nan to -1 (in one line of code)? [duplicate]
This question already has an answer here:
finding and replacing 'nan' with a number
2 answers
convert nan value to zero
9 answers
I have z = np.array([4.4, 3, 0, np.nan, -1, 6])
and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.
I tried:
np.where(z == np.nan, -1, z)
nothing happens
np.nan_to_num(z, -1)
changes to zeros
z = [-1 if np.nan(x) else x for x in z]
TypeError: 'float' object is not callable
Why such earsy things can't be just as pure easy? I must use numpy only.
python numpy
marked as duplicate by jedwards
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();
}
);
});
});
Jan 20 at 14:22
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:
finding and replacing 'nan' with a number
2 answers
convert nan value to zero
9 answers
I have z = np.array([4.4, 3, 0, np.nan, -1, 6])
and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.
I tried:
np.where(z == np.nan, -1, z)
nothing happens
np.nan_to_num(z, -1)
changes to zeros
z = [-1 if np.nan(x) else x for x in z]
TypeError: 'float' object is not callable
Why such earsy things can't be just as pure easy? I must use numpy only.
python numpy
marked as duplicate by jedwards
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();
}
);
});
});
Jan 20 at 14:22
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.
In your list comprehension, changingnp.nan(x)
tonp.isnan(x)
will work. (Alternatively, and possibly faster, would be:z[np.isnan(z)] = -1
).
– jedwards
Jan 20 at 14:19
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25
add a comment |
This question already has an answer here:
finding and replacing 'nan' with a number
2 answers
convert nan value to zero
9 answers
I have z = np.array([4.4, 3, 0, np.nan, -1, 6])
and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.
I tried:
np.where(z == np.nan, -1, z)
nothing happens
np.nan_to_num(z, -1)
changes to zeros
z = [-1 if np.nan(x) else x for x in z]
TypeError: 'float' object is not callable
Why such earsy things can't be just as pure easy? I must use numpy only.
python numpy
This question already has an answer here:
finding and replacing 'nan' with a number
2 answers
convert nan value to zero
9 answers
I have z = np.array([4.4, 3, 0, np.nan, -1, 6])
and just can't find any quick and friendly solution for easy replacement. I can't believe it's sometimes such not user friendly language.
I tried:
np.where(z == np.nan, -1, z)
nothing happens
np.nan_to_num(z, -1)
changes to zeros
z = [-1 if np.nan(x) else x for x in z]
TypeError: 'float' object is not callable
Why such earsy things can't be just as pure easy? I must use numpy only.
This question already has an answer here:
finding and replacing 'nan' with a number
2 answers
convert nan value to zero
9 answers
python numpy
python numpy
asked Jan 20 at 14:13
Peter.kPeter.k
446412
446412
marked as duplicate by jedwards
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();
}
);
});
});
Jan 20 at 14:22
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 jedwards
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();
}
);
});
});
Jan 20 at 14:22
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.
In your list comprehension, changingnp.nan(x)
tonp.isnan(x)
will work. (Alternatively, and possibly faster, would be:z[np.isnan(z)] = -1
).
– jedwards
Jan 20 at 14:19
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25
add a comment |
In your list comprehension, changingnp.nan(x)
tonp.isnan(x)
will work. (Alternatively, and possibly faster, would be:z[np.isnan(z)] = -1
).
– jedwards
Jan 20 at 14:19
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25
In your list comprehension, changing
np.nan(x)
to np.isnan(x)
will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1
).– jedwards
Jan 20 at 14:19
In your list comprehension, changing
np.nan(x)
to np.isnan(x)
will work. (Alternatively, and possibly faster, would be: z[np.isnan(z)] = -1
).– jedwards
Jan 20 at 14:19
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25
add a comment |
1 Answer
1
active
oldest
votes
Use np.isnan(x)
. You can replace like this:
z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)
# [ 4.4 3. 0. -1. -1. 6. ]
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use np.isnan(x)
. You can replace like this:
z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)
# [ 4.4 3. 0. -1. -1. 6. ]
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
add a comment |
Use np.isnan(x)
. You can replace like this:
z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)
# [ 4.4 3. 0. -1. -1. 6. ]
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
add a comment |
Use np.isnan(x)
. You can replace like this:
z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)
# [ 4.4 3. 0. -1. -1. 6. ]
Use np.isnan(x)
. You can replace like this:
z = np.array([4.4, 3, 0, np.nan, -1, 6])
z[np.isnan(z)] = -1
print(z)
# [ 4.4 3. 0. -1. -1. 6. ]
answered Jan 20 at 14:19
JeppeJeppe
678615
678615
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
add a comment |
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
God bless you. Thanks for very quick response.
– Peter.k
Jan 20 at 14:22
add a comment |
In your list comprehension, changing
np.nan(x)
tonp.isnan(x)
will work. (Alternatively, and possibly faster, would be:z[np.isnan(z)] = -1
).– jedwards
Jan 20 at 14:19
Maybe it's duplicated but I also tried to show Python 4 needs to have better implementations for some base functions.
– Peter.k
Jan 20 at 14:25