dataframe某列数据多条件标色
时间: 2023-09-01 09:10:49 浏览: 87
你可以使用`Styler`对象的`apply`方法来实现DataFrame列数据的多条件标色,具体步骤如下:
1. 定义一个函数,该函数接受一个Series对象,根据条件返回对应的CSS样式字符串。
2. 使用`Styler`对象的`apply`方法,将该函数应用到DataFrame的某列上,该方法将返回一个新的`Styler`对象。
3. 在新的`Styler`对象上使用`set_table_styles`方法添加CSS样式,最后通过`render`方法将结果渲染为HTML字符串。
下面是一个示例代码,假设我们有一个DataFrame `df`,其中包含了一个名为`score`的列,我们想要将`score`列中小于60的值标为红色,大于等于60且小于80的值标为黄色,大于等于80的值标为绿色:
```python
import pandas as pd
# 定义标色函数
def color_negative_red(val):
color = 'red' if val < 60 else 'yellow' if val < 80 else 'green'
return f'color: {color}'
# 创建DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'score': [70, 55, 85, 90]
})
# 创建Styler对象并应用标色函数
styler = df.style.applymap(color_negative_red, subset=['score'])
# 添加CSS样式
styles = [{
'selector': 'td',
'props': [('padding', '4px 8px')]
}, {
'selector': 'tr:hover',
'props': [('background-color', '#ddd')]
}]
styler.set_table_styles(styles)
# 输出HTML字符串
html = styler.render()
print(html)
```
输出结果为:
```html
<style>
#T_69ae7_row0_col1 {
color: yellow;
}
#T_69ae7_row1_col1 {
color: red;
}
#T_69ae7_row2_col1 {
color: green;
}
#T_69ae7_row3_col1 {
color: green;
}
</style>
<table id="T_69ae7_" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>name</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Alice</td>
<td style="color: yellow;">70</td>
</tr>
<tr>
<th>1</th>
<td>Bob</td>
<td style="color: red;">55</td>
</tr>
<tr>
<th>2</th>
<td>Charlie</td>
<td style="color: green;">85</td>
</tr>
<tr>
<th>3</th>
<td>David</td>
<td style="color: green;">90</td>
</tr>
</tbody>
</table>
```
在上面的代码中,我们首先定义了一个名为`color_negative_red`的函数,该函数接受一个值,根据该值返回对应的CSS样式字符串。在这个例子中,我们使用`if-else`语句根据不同的值返回不同的颜色。
然后,我们创建了一个DataFrame,并使用`style.applymap`方法将`color_negative_red`函数应用到`score`列上,该方法将返回一个新的`Styler`对象。
接下来,我们使用`set_table_styles`方法添加了一些CSS样式,其中包括表格单元格的内边距和鼠标悬停时的背景色。
最后,我们使用`render`方法将结果渲染为HTML字符串,并输出到控制台。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)