I'm trying to print a data frame to a log file, but the columns are misaligned. I came up with this solution to get the dataframe columns and row as a list, and then convert the columns and floating point data to strings, but there must be a better way to do this.
logging.error(f'Quality Metrics for Reconstructed File are Lower than Reference!!!')
print(df.shape)
columns = df_res.columns
msg = ' '.join(columns)
print(msg)
logging.error(msg)
data = df_res.iloc[0,:].tolist()
msg = ' '.join(map(str, data))
print(msg)
logging.error(msg)```
This is what the log file looks like.
2022-08-24 16:03:49,084 [ERROR] mse_avg mse_r mse_g mse_b psnr_avg psnr_r psnr_g psnr_b
2022-08-24 16:03:51,263 [ERROR] 0.02 0.01 0.03 0.01 65.44 67.82 62.78 67.82
You can see that the columns are not aligned correctly. Any advice on a better way to do this so that my log output is properly formatted?
CodePudding user response:
This example is using str.format
to properly format column header and values from first row of dataframe:
max_column_len = len(max(df.columns, key=len))
fmt = "{{:>{}}} ".format(max_column_len) * len(df.columns)
columns = fmt.format(*df.columns)
first_row = fmt.format(*df.iloc[0])
print(columns) # <--- you can use `columns` in your logging function
print(first_row) # <--- ...
Prints:
mse_avg mse_r mse_g mse_b psnr_avg psnr_r psnr_g psnr_b
0.02 0.01 0.03 0.01 65.44 67.82 62.78 67.82
Dataframe used:
mse_avg mse_r mse_g mse_b psnr_avg psnr_r psnr_g psnr_b
0 0.02 0.01 0.03 0.01 65.44 67.82 62.78 67.82
Another solution:
# convert dataframe string and then split it:
for line in str(df).splitlines():
print(line)
CodePudding user response:
You could use this pandas method:
string = df.to_csv(sep='\t')
It will separate the entries with a tabulator, which should appear aligned in most text editors.