[split] Python Question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Python Question (/thread-24698.html) Pages:
1
2
|
[split] Python Question - osreddy - Feb-28-2020 how do I convert df.info() and df.describe() in to PDF in Python RE: [split] Python Question - Larz60+ - Feb-28-2020 one method: use two step process: first convert dataframe to html using pandas.DataFrame.to_html: https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.to_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) RE: [split] Python Question - osreddy - Feb-29-2020 (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 RE: [split] Python Question - Larz60+ - Feb-29-2020 Quote:it is not workingCould you please elaborate. RE: [split] Python Question - jefsummers - Feb-29-2020 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__ RE: [split] Python Question - snippsat - Feb-29-2020 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 :
RE: [split] Python Question - osreddy - Mar-26-2020 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 RE: [split] Python Question - buran - Mar-26-2020 >>> 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
RE: [split] Python Question - osreddy - Mar-26-2020 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 RE: [split] Python Question - buran - Mar-26-2020 This looks like XY problem. What do you want to do? I doubt you want to get name df_name , which is str .
|