-
Notifications
You must be signed in to change notification settings - Fork 711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to log a Pandas DataFrame #1265
Comments
Hi @vladjohnson. Can you please clarify the "spacing issues" you're encountering? The following code produces a readable table in the logs (note I just added import pandas
from loguru import logger
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "Los Angeles", "Chicago"],
}
df = pandas.DataFrame(data)
logger.info("Created DataFrame:\n{}", df)
|
Data practitioner here. I am heavily working with pandas DataFrames and I noticed something weird when using the The first thing is: Under no circumstance should you try to log the full-format DataFrame. This is just not what logging is for. The entire Python script is attached as text file: main.txt. I used the TMDB dataset at TMDB Movies, version 349. Libraries versions
TL;DRJust calling the DataFrame or using the Using the Default pandas outputThe way suggested by @Delgan. logger.info("TMDB dataset:\n{}", tmdb) This is going to produce something like:
|
Thanks for these extensive guidelines, @CesarArroyo09! Note that to capture the output |
My pleasure @Delgan! @vladjohnson Hope the information is clarifying! I would only add that if we are collecting the results using the So if: import io
from loguru import logger
buffer = io.StringIO()
# Assume df1 and df2 already exists
df1.info(buf=buffer)
logger.info(buffer.getvalue()) # This shows the result of `df1.info()`
df2.info(buf=buffer) # This will append the print to buffer
logger.info(buffer.getvalue()) # This logs both `df1.info()` and `df2.info()` This can be avoided in 2 ways:
|
What would be the best way to log a Pandas DataFrame without experiencing spacing issues? Thank you!
The text was updated successfully, but these errors were encountered: