Search code examples
pythonpython-3.xpandasloggingf-string

python 3.6+ logger to log pandas dataframe - how to indent the entire dataframe?


I need to use python logging module to log pandas dataframe. I need the entire dataframe (all rows) indented equally.

Below is the simple desired output:

Test Dataframe Output Below:

       col1  col2
    0     1     3
    1     2     4

However, I am getting the following output where the indentation is only applied to the first row of the dataframe:

Test Dataframe Output Below:

       col1  col2
0     1     3
1     2     4

Sample code I am running is:

import pandas as pd
import logging

# sample dataframe
test_df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

# logging set up
logging.basicConfig(level=logging.INFO)
logging.getLogger().handlers.clear()
c_handler = logging.StreamHandler()
c_handler.setFormatter(logging.Formatter('%(message)s'))
logging.getLogger().addHandler(c_handler)

# log the pandas dataframe to console
logging.info(f'\tTest Dataframe Output Below:')
logging.info(f'\n\t\t{test_df}')
logging.info(f'{test_df}')

Any help will be greatly appreciated!


Solution

  • logging.info('\t'+ test_df.to_string().replace('\n', '\n\t'))