Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Python Question
#1
how do I convert df.info() and df.describe() in to PDF in Python
Reply
#2
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)
Reply
#3
(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, use pip 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
Reply
#4
Quote:it is not working
Could you please elaborate.
Reply
#5
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__
Reply
#6
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
Reply
#7
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
Reply
#8
>>> 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

Reply
#9
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
Reply
#10
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

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] [split] New to the forum, how to post a question? karnik 2 1,244 Feb-12-2022, 03:45 PM
Last Post: deanhystad
  [split] question about list comprehension Armin 17 5,521 Jan-29-2020, 04:32 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020