使用日志记录和 pandas 记录数据帧

问题描述 投票:0回答:1

我使用

pandas
来操作数据帧,并使用
logging
将中间结果以及警告和错误记录到单独的日志文件中。我还需要将一些中间数据帧打印到同一个日志文件中。具体来说,我想:

  • 将数据帧打印到与其余
    logging
    消息
    相同的日志文件中(以确保更轻松地调试并避免编写许多中间文件,就像使用文件目标调用
    to_csv
    的情况一样),
  • 使用
    logging
    级别
    (例如
    DEBUG
    INFO
    )控制日志记录的详细程度(通常这样做),与其他日志消息的详细程度共享(包括那些与数据帧不相关的消息) ).
  • 控制日志记录的详细程度
  • (在更精细的级别上)使用单独的变量来确定要打印的数据帧的行数。
  • 每行打印 1 行,列对齐,每行前面都有典型的日志记录元数据
  • ,例如 240102 10:58:20 INFO:
    
    
  • 我能想到的最好的就是下面的代码,这有点太冗长了。有没有一种更简单、更Pythonic的方法来记录数据帧切片?

注:

请附上用法示例。

示例:

import io import logging import pandas as pd # Print into log this many lines of several intermediate dataframes, # set to 20 or so: MAX_NUM_DF_LOG_LINES = 4 logging.basicConfig( datefmt = '%y%m%d %H:%M:%S', format = '%(asctime)s %(levelname)s: %(message)s') logger = logging.getLogger(__name__) # Or logging.DEBUG, etc: logger.setLevel(level = logging.INFO) # Example of a simple log message: logger.info('Reading input.') TESTDATA=""" enzyme regions N length AaaI all 10 238045 AaaI all 20 170393 AaaI captured 10 292735 AaaI captured 20 229824 AagI all 10 88337 AagI all 20 19144 AagI captured 10 34463 AagI captured 20 19220 """ df = pd.read_csv(io.StringIO(TESTDATA), sep='\s+') # ...some code.... # Example of a log message with a chunk of a dataframe, here, using # `head` (but this can be another method that slices a dataframe): logger.debug('less important intermediate results: df:') for line in df.head(MAX_NUM_DF_LOG_LINES).to_string().splitlines(): logger.debug(line) # ...more code.... logger.info('more important intermediate results: df:') for line in df.head(MAX_NUM_DF_LOG_LINES).to_string().splitlines(): logger.info(line) # ...more code....

打印:

240102 10:58:20 INFO: Reading input. 240102 10:58:20 INFO: more important intermediate results: df: 240102 10:58:20 INFO: enzyme regions N length 240102 10:58:20 INFO: 0 AaaI all 10 238045 240102 10:58:20 INFO: 1 AaaI all 20 170393 240102 10:58:20 INFO: 2 AaaI captured 10 292735 240102 10:58:20 INFO: 3 AaaI captured 20 229824

相关:

这些都没有完成我尝试做的事情,但它已经越来越接近了:

    如何使用python日志模块打印多行日志?
  • 请参阅此注释,它很简洁,但不太Pythonic,因为它从列表理解内部调用
      print
    • ,然后丢弃结果:
      “请注意,由于映射是惰性的,后者仅适用于py2;你可以在 py3 上执行 
      [logger.info(line) for line in 'line 1\nline 2\nline 3'.splitlines()]。 – 九八,2021 年 6 月 22 日 16:30"。
      
      
      此外,
    • Qeek
    • 接受的答案也存在问题:(a)它缺乏动态定义写入日志的最大数据帧行数的功能(每个脚本定义一次,而不是每次调用记录器); (b) 没有使用示例,因此不清楚。
  • 写入或记录 pandas Dataframe 的打印输出
  • - 这会打印类似这样的内容,即每行开头缺少时间戳+日志记录级别元数据:
  • 240102 12:27:19 INFO: dataframe head - enzyme regions N length 0 AaaI all 10 238045 1 AaaI all 20 170393 2 AaaI captured 10 292735 ...
    如何将数据帧记录到输出文件
  • - 与之前的答案相同。
python pandas dataframe logging pretty-print
1个回答
0
投票

def log_df(level, df, n_rows, header): if isinstance(level, str): level = getattr(logging, level) logger.log(level, header) for line in df.head(n_rows).to_string().splitlines(): logger.log(level, line) log_df("INFO", df, MAX_NUM_DF_LOG_LINES, 'more important intermediate results: df:')

© www.soinside.com 2019 - 2024. All rights reserved.