在 PrettyTable 中用 add_columns 创建 table
Create table with add_columns in PrettyTable
我正在尝试使用 PrettyTable 创建 table 性能指标。给定 table 布局,最好使用度量列表和带有 add_column
选项的值列表按列填充 table。但是,当我尝试这样做时,输出在一行中给出了所有列表值:
from prettytable import PrettyTable
mse=123
mae=456
r2=789
performance_metrics=['Mean Squared Error', 'Mean Absolute Error', 'R-Squared']
values=[mse, mae, r2]
#CREATE TABLE
table = PrettyTable()
table.title = 'Model Performance'
column_names=['Metric', 'Value']
table.add_column(column_names[0],[performance_metrics])
table.add_column(column_names[1],[values])
print(table)
+------------------------------------------------------------------------------+
| Model Performance |
+------------------------------------------------------------+-----------------+
| Metric | Value |
+------------------------------------------------------------+-----------------+
| ['Mean Squared Error', 'Mean Absolute Error', 'R-Squared'] | [123, 456, 789] |
+------------------------------------------------------------+-----------------+
如何在不单独添加每一行的情况下解决这个问题?
更新:回读我意识到我忘记附加所需输出的问题,这显然类似于以下内容:
+------------------------------------------------------------------------------+
| Model Performance |
+------------------------------------------------------------+-----------------+
| Metric | Value |
+------------------------------------------------------------+-----------------+
| Mean Squared Error | 123 |
+------------------------------------------------------------+-----------------+
| Mean Absolute Error | 456 |
+------------------------------------------------------------+-----------------+
| R-Squared | 789 |
+------------------------------------------------------------+-----------------+
您必须尝试对该操作使用循环,此时您要将整个列表添加到一列:
from prettytable import PrettyTable
mse=123
mae=456
r2=789
performance_metrics=['Mean Squared Error', 'Mean Absolute Error', 'R-Squared']
values=[mse, mae, r2]
#CREATE TABLE
table = PrettyTable()
table.title = 'Model Performance'
column_names=['Metric', 'Value']
table.field_names = [column_names[0], column_names[1]]
你必须使用 add_row 来滥用相同列的乘法:
for i in range(len(performance_metrics)):
table.add_row([performance_metrics[i],values[i]])
print(table)
输出:
+-----------------------------+
| Model Performance |
+---------------------+-------+
| Metric | Value |
+---------------------+-------+
| Mean Squared Error | 123 |
| Mean Absolute Error | 456 |
| R-Squared | 789 |
+---------------------+-------+
我正在尝试使用 PrettyTable 创建 table 性能指标。给定 table 布局,最好使用度量列表和带有 add_column
选项的值列表按列填充 table。但是,当我尝试这样做时,输出在一行中给出了所有列表值:
from prettytable import PrettyTable
mse=123
mae=456
r2=789
performance_metrics=['Mean Squared Error', 'Mean Absolute Error', 'R-Squared']
values=[mse, mae, r2]
#CREATE TABLE
table = PrettyTable()
table.title = 'Model Performance'
column_names=['Metric', 'Value']
table.add_column(column_names[0],[performance_metrics])
table.add_column(column_names[1],[values])
print(table)
+------------------------------------------------------------------------------+
| Model Performance |
+------------------------------------------------------------+-----------------+
| Metric | Value |
+------------------------------------------------------------+-----------------+
| ['Mean Squared Error', 'Mean Absolute Error', 'R-Squared'] | [123, 456, 789] |
+------------------------------------------------------------+-----------------+
如何在不单独添加每一行的情况下解决这个问题?
更新:回读我意识到我忘记附加所需输出的问题,这显然类似于以下内容:
+------------------------------------------------------------------------------+
| Model Performance |
+------------------------------------------------------------+-----------------+
| Metric | Value |
+------------------------------------------------------------+-----------------+
| Mean Squared Error | 123 |
+------------------------------------------------------------+-----------------+
| Mean Absolute Error | 456 |
+------------------------------------------------------------+-----------------+
| R-Squared | 789 |
+------------------------------------------------------------+-----------------+
您必须尝试对该操作使用循环,此时您要将整个列表添加到一列:
from prettytable import PrettyTable
mse=123
mae=456
r2=789
performance_metrics=['Mean Squared Error', 'Mean Absolute Error', 'R-Squared']
values=[mse, mae, r2]
#CREATE TABLE
table = PrettyTable()
table.title = 'Model Performance'
column_names=['Metric', 'Value']
table.field_names = [column_names[0], column_names[1]]
你必须使用 add_row 来滥用相同列的乘法:
for i in range(len(performance_metrics)):
table.add_row([performance_metrics[i],values[i]])
print(table)
输出:
+-----------------------------+
| Model Performance |
+---------------------+-------+
| Metric | Value |
+---------------------+-------+
| Mean Squared Error | 123 |
| Mean Absolute Error | 456 |
| R-Squared | 789 |
+---------------------+-------+