Feb-28-2020, 07:06 AM
Pages: 1 2
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
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:
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, usepip install pdfkit
)
import pdfkit as pdk pdk.from_file(path, htmlfilename)
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
Alternative -
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.
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__
Feb-29-2020, 09:19 PM
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
:Output:<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
one 4 non-null float64
two 4 non-null float64
dtypes: float64(2)
memory usage: 104.0 bytes
one two
count 4.000000 4.000000
mean 2.500000 2.500000
std 1.290994 1.290994
min 1.000000 1.000000
25% 1.750000 1.750000
50% 2.500000 2.500000
75% 3.250000 3.250000
max 4.000000 4.000000
Mar-26-2020, 03:58 PM
How can we extract data frame name ?
I have used below code
But my requirement is : df_name
because want to pass the data frame as parameter, please help
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
Mar-26-2020, 04:07 PM
>>>
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 isMar-26-2020, 04:10 PM
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
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
.Pages: 1 2