python - 根据列值对行进行条件样式
问题描述
我想根据列的值设置行的样式,我目前有以下代码:
import dash
import dash_table
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
app = dash.Dash(__name__)
print([{"name": i, "id": i} for i in df.columns])
app.layout = dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("rows"),
style_data_conditional=[
{
'if': {
'column_id': 'Number of Solar Plants',
'filter': '"Number of Solar Plants" > num(100)'
},
'backgroundColor': '#3D9970',
'color': 'white',
}
],
)
if __name__ == '__main__':
app.run_server(debug=True)
这会产生以下结果:
但我真正想要的是将行(在这种情况下为 tr 1 和 8)设置为绿色背景,而不仅仅是单元格。
我能做些什么来实现这一目标?
解决方案
column_id
要解决您的问题,您只需删除style_data_conditional
. 所以所有的行都将被涂成绿色。
你应该做这个:
style_data_conditional=[
{
'if': {
'filter': '"Number of Solar Plants" > num(100)'
},
'backgroundColor': '#3D9970',
'color': 'white',
}
]
推荐阅读
- android - 使用 Android.mk mm 命令构建的 APK 安装后未在设备中运行
- reactjs - 如何将 ref 传递给包装在高阶组件中的子组件 - TypeScript 错误
- vcloud-director-rest-api - terraform 的 iniscript 功能在 vcloud director 上不起作用
- vhdl - 测试/模拟在 FPGA 上硬编码的组件
- python - 如何检查拆分的列值是否在另一列中?
- flutter - 在 Flutter 中使用 Firebase 进行电子邮件验证
- sorting - 删除已排序 Go 切片重复项的最快方法
- vert.x - mysql客户端preparedQuery没有关闭
- c# - WPF:为什么我的 ComboBox SelectedItem 不显示?
- c++ - 如何使用 CodeBlocks 配置 Catch2