I am trying to create a table of performance metrics using PrettyTable. Given the table layout, it would be best to fill the table by column using a list of metrics and a list of values with the add_column
option. However, when I try to do that, the output gives all the lists values in a single row:
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] |
+------------------------------------------------------------+-----------------+
How can I fix this possibly without resorting to adding each row on its own?
UPDATE: Reading back the question I realized I forgot to attach the desired output, which is obviously something like the following:
+------------------------------------------------------------------------------+
| Model Performance |
+------------------------------------------------------------+-----------------+
| Metric | Value |
+------------------------------------------------------------+-----------------+
| Mean Squared Error | 123 |
+------------------------------------------------------------+-----------------+
| Mean Absolute Error | 456 |
+------------------------------------------------------------+-----------------+
| R-Squared | 789 |
+------------------------------------------------------------+-----------------+
You have to try use loop for that operation, at this moment u re adding whole list to one column:
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]]
u have to use add_row to abuse multipling same columns:
for i in range(len(performance_metrics)):
table.add_row([performance_metrics[i],values[i]])
print(table)
Output:
+-----------------------------+
| Model Performance |
+---------------------+-------+
| Metric | Value |
+---------------------+-------+
| Mean Squared Error | 123 |
| Mean Absolute Error | 456 |
| R-Squared | 789 |
+---------------------+-------+