array([['B1', 'B3', 'Unnamed: 2', 'B4', 'Unnamed: 4', 'Unnamed: 5'],
['B2', nan, 'B5', nan, nan, nan],
[nan, nan, nan, nan, nan, 'B6']], dtype=object)
提取 非nan值
这个错误是因为你在使用`numpy.isnan()`方法时,输入了不支持的数据类型,比如对象或字符串³。要解决这个错误,你可以使用`pandas.isnull()`方法,或者将数组中的值转换为浮点数³。例如:
```python
import numpy as np
import pandas as pd
# 创建一个包含NaN和字符串的数组
a = np.array([1, np.nan, 'B', np.nan, 'C'])
# 使用pandas.isnull获取一个布尔数组,该数组表示a中的元素是否为NaN
isnull = pd.isnull(a)
# 使用~运算符获取一个布尔数组,该数组表示a中的元素是否不为NaN
notnull = ~isnull
# 使用布尔索引来获取a中的非NaN元素
a_notnull = a[notnull]
print(a_notnull) # 输出:[1 'B' 'C']
```
在这个代码中,`pd.isnull(a)`返回一个布尔数组,如果`a`中的元素为NaN,那么对应的布尔值为True³。然后,`~isnull`返回一个布尔数组,如果`a`中的元素不为NaN,那么对应的布尔值为True³。最后,`a[notnull]`返回一个新的数组,该数组只包含`a`中的非NaN元素³。
希望这个解释对你有所帮助!
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
在Python中,你可以使用NumPy的np.isnan
函数和布尔索引来提取非NaN值。以下是一个示例:
import numpy as np
# 创建一个包含NaN的数组
a = np.array([['B1', 'B3', 'Unnamed: 2', 'B4', 'Unnamed: 4', 'Unnamed: 5'],
['B2', np.nan, 'B5', np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan, np.nan, np.nan, 'B6']], dtype=object)
# 使用np.isnan获取一个布尔数组,该数组表示a中的元素是否为NaN
isnan = np.isnan(a)
# 使用~运算符获取一个布尔数组,该数组表示a中的元素是否不为NaN
notnan = ~isnan
# 使用布尔索引来获取a中的非NaN元素
a_notnan = a[notnan]
print(a_notnan) # 输出:['B1' 'B3' 'Unnamed: 2' 'B4' 'Unnamed: 4' 'Unnamed: 5' 'B2' 'B5' 'B6']
在这个代码中,np.isnan(a)
返回一个布尔数组,如果a
中的元素为NaN,那么对应的布尔值为True。然后,~isnan
返回一个布尔数组,如果a
中的元素不为NaN,那么对应的布尔值为True。最后,a[notnan]
返回一个新的数组,该数组只包含a
中的非NaN元素。
希望这个解释对你有所帮助!
(1) TypeError: ufunc 'isnan' not supported for the input types. https://bobbyhadz.com/blog/type-error-ufunc-isnan-not-supported-for-input-types.
(2) TypeError: ufunc 'isnan' not supported for the input types, and the .... https://stackoverflow.com/questions/52657223/typeerror-ufunc-isnan-not-supported-for-the-input-types-and-the-inputs-could.
(3) ufunc 'isnan' not supported for the input types - Stack Overflow. https://stackoverflow.com/questions/69542196/ufunc-isnan-not-supported-for-the-input-types.
(4) TypeError: ufunc 'isnan' not supported for the input types, - seaborn .... https://stackoverflow.com/questions/43523115/typeerror-ufunc-isnan-not-supported-for-the-input-types-seaborn-heatmap.
(5) Python Numpy TypeError: ufunc 'isfinite' not supported for the input types. https://stackoverflow.com/questions/40809503/python-numpy-typeerror-ufunc-isfinite-not-supported-for-the-input-types.