Feb-28-2020, 07:06 AM
how do I convert df.info() and df.describe() in to PDF in Python
[split] Python Question
|
Feb-28-2020, 07:06 AM
how do I convert df.info() and df.describe() in to PDF in Python
Feb-28-2020, 02:42 PM
one method: use two step process:
first convert dataframe to html using pandas.DataFrame.to_html: https://pandas.pydata.org/pandas-docs/ve..._html.html next convert html file to pdf using pdfkit (if you need to install, use pip install pdfkit )import pdfkit as pdk pdk.from_file(path, htmlfilename)
Feb-29-2020, 05:28 AM
(Feb-28-2020, 02:42 PM)Larz60+ Wrote: one method: use two step process: I have tried like below import pandas as pd df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3")) df1 =df.to_html(df.info()) pdk.from_file('htm.pdf', df1) it is not working
Feb-29-2020, 11:26 AM
Quote:it is not workingCould you please elaborate.
Feb-29-2020, 07:08 PM
(This post was last modified: Feb-29-2020, 07:09 PM by jefsummers.)
Alternative -
import sys import pandas as pd sys.stdout = open('xxxstdout.txt','w') df = pd.DataFrame() print(df.describe)Then use a library function to convert the text file to a pdf. Changing the value of stdout redirects all print (and the prompt part of input) statements to the new file. __stdout__ is the value of stdout when the program starts, so you do want to put it back once you are done with the text file generation. sys.stdout = sys.__stdout__
A working test,here using
buf parameter to get sys.stout from info(),i guess @jefsummers advice would work to.import pandas as pd import pdfkit # Make DataFrame d = { 'one': [1., 2., 3., 4.], 'two': [4., 3., 2., 1.] } df = pd.DataFrame(d) describe = df.describe() info = df.info(buf=open('info.txt', 'w')) with open('describe.txt', 'w', encoding='utf-8') as f: f.write(describe.to_string()) # Make one file file_names = ['info.txt', 'describe.txt'] with open('to_pdf.txt', 'w') as f_out: for fname in file_names: with open(fname) as f: f_out.write(f'{f.read()}\n') # To pdf pdfkit.from_file("to_pdf.txt", "info.pdf")In info.pdf :
How can we extract data frame name ?
I have used below code import pandas as pd import numpy as np df_name = pd.DataFrame( data=np.ones([4,4]) ) def get_df_name(df): name =[x for x in globals() if globals()[x] is df_name][0] return name get_df_name(df)>>> 'df_name' But my requirement is : df_name because want to pass the data frame as parameter, please help >>> indicate you work in interactive mode.You want to print the result print(get_df_name(df))if you were not working in interactive mode, but had the code in py file and execute it, there would be no output without the print. Note, I am not sure why would you need the df_name string, nor what df that you pass when calling the function is
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
df_name = pd.DataFrame( data=np.ones([4,4]) )
def get_df_name(df): name =[x for x in globals() if globals()[x] is df][0] return name get_df_name(df_name) output is : 'df_name' But cannot use print(get_df_name(df_name)) also, because need to pass in another function
Mar-26-2020, 04:15 PM
This looks like XY problem. What do you want to do? I doubt you want to get name
df_name , which is str .
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
[split] [split] New to the forum, how to post a question? | karnik | 2 | 1,988 |
Feb-12-2022, 03:45 PM Last Post: deanhystad |
|
[split] question about list comprehension | Armin | 17 | 8,407 |
Jan-29-2020, 04:32 PM Last Post: Clunk_Head |