selected_ratio = ratio < 0 selected_ratio = selected_ratio.apply(lambda x: x.astype(int))报错'bool' object has no attribute 'astype'
时间: 2024-02-06 20:13:07 浏览: 26
这个错误提示表明 `selected_ratio` 是一个布尔类型的 DataFrame,而该类型没有 `astype()` 方法。为了将布尔类型的 DataFrame 转换为 0/1 数组,你可以使用 `astype(int)` 或者 `astype('int')` 方法来实现,具体如下:
```python
# 将 selected_ratio 转换为 0/1 数组
selected_ratio = selected_ratio.astype(int)
# 或者
selected_ratio = selected_ratio.astype('int')
```
这样就可以将 selected_ratio 的元素从布尔类型转换为整型了。
相关问题
null_ratio = null_counts.apply(lambda x: 1-x / len(null_counts_group))怎么变成百分数
您可以在 lambda 表达式中将计算结果乘以 100,然后在字符串格式化时添加百分号,以将其转换为百分数。以下是一个示例代码:
```python
null_ratio = null_counts.apply(lambda x: (1-x / len(null_counts_group)) * 100)
percent_null_ratio = null_ratio.apply(lambda x: "{:.2f}%".format(x))
print(percent_null_ratio)
```
在上面的代码中,我们首先将计算结果乘以 100,然后使用 lambda 表达式将每个值格式化为带有两位小数的百分数字符串。最终输出结果为每个列的空值比例的百分数字符串列表。
null_ratio = null_counts.apply(lambda x: 1-x / len(null_counts_group))怎么输出为百分数
您可以在 lambda 表达式中将计算结果乘以 100,以将其转换为百分数。以下是一个示例代码:
```python
null_ratio = null_counts.apply(lambda x: (1 - x / len(null_counts_group)) * 100)
print(null_ratio)
```
在上面的代码中,我们首先将计算结果乘以 100,以将其转换为百分数。最终输出结果为每个列的空值比例的百分数列表。