Ad
Pythonic Way Of Replacing Nan Sub-array With Array Of Same Size
Suppose i have given a numpy
array like this:
a = np.array([1, 2, 3, 4, np.nan, 5, np.nan, 6, np.nan])
# [1, 2, 3, 4, nan, 5, nan, 6, nan]
I know the number of nan
values in the array and have the according array for replacement, e.g.:
b = np.array([12, 13, 14])
# [12, 13, 14]
What is the pythonic way of substituting the array b
for all the nan
value, such that I get the reult:
[1, 2, 3, 4, 12, 5, 13, 6, 14]
Ad
Answer
Perform boolean indexing on a
using np.isnan
and replace with b
as:
a[np.isnan(a)] = b
print(a)
# array([ 1., 2., 3., 4., 12., 5., 13., 6., 14.])
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad