- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from datetime import datetime
- #Download the data
- import urllib.request
- urllib.request.urlretrieve("https://www.dropbox.com/s/2dqqv1984h3mb5s/Placement_Data_Full_Class.csv?dl=1", "Placement_Data_Full_Class.csv")
- data = pd.read_csv("Placement_Data_Full_Class.csv")
- #Show the first 5 top rows
- #print(data.head(5))
- #Pivoting The Data:
- #Firstly, check the rows and columns you want to include in pivot table
- pivot=pd.pivot_table(
- data,
- values="sl_no",
- index=["degree_t","specialisation"],
- columns=["status","gender"], aggfunc=np.sum
- )
- pivot=pivot.fillna(0).astype(np.int64)
- #print(pivot)
- #We can achieve the same by
- pivot=data.pivot_table(index=["degree_t","specialisation"],
- columns=["status","gender"],
- values="sl_no",aggfunc=np.sum).fillna(0)
- #print(pivot)
- #Let us make columns more readable
- new_columns = [
- 'Unplaced_Females',
- 'Placed_Females',
- 'Unplaced_Males',
- 'Placed_Males'
- ]
- pivot.columns = new_columns
- print(pivot)
- #Let us plot the data
- plot = pivot.plot(kind="bar",figsize=(10.5,6))
- plot.tick_params(rotation=40)
- plot
- plt.title("Placement Bar Chart")
- plt.tight_layout()
- plt.show()
- #Convert this report into HTML, itcan be styled by using CSS
- #Saving plot image to local file
- image = plot.get_figure().savefig('plot.png')
- image_tag = '<img src="plot.png" class="center">'
- #writing HTML Content
- heading = '<h1> Automated Report </h1>'
- subheading = '<h3> Sample Report for Placement </h3>'
- #Using .now() from datetime library to add Time stamp
- now = datetime.now()
- current_time = now.strftime("%m/%d/%Y %H:%M:%S")
- header = '<div class="top">' + heading + subheading +'</div>'
- footer = '<div class="bottom"> <h3 style="text-align: center;"> This Report has been Generated on '+ current_time +'</h3> </div>'
- content = '<div class="table"> '+pivot.to_html()+' </div> <div class="chart"> '+ image_tag +'</div>'
- #Concating everything to a single string
- html = header + content + footer
- # Adding CSS to HTML
- css = """<style> body {\n text-align:center; \n}\n table{\n margin:0px auto;\n}</style>"""
- html = html + css
- #Writing the file
- with open("report.html","w+") as file:
- file.write(html)
- #Produce pdf report
- #from weasyprint import HTML
- #HTML('report.html').write_pdf('report.pdf')
-
- Active Topics
-
-
- by Eli 2 hours ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 273 Views 6482
- by Eli 22 hours ago Chat With ChatGPT - An Interactive Conversational AI View the latest post Replies 21 Views 22640
- by Eli 5 days ago Re: What is in Your Mind? View the latest post Replies 658 Views 271335
- by Eli 5 days ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 29 Views 42677
- by Eli 5 days ago Russia Invades Ukraine View the latest post Replies 642 Views 209042
- by Eli 6 days ago Mission Control Live: NASA InSight Mars Landing Live Coverage View the latest post Replies 32 Views 12957
- by Eli 6 days ago Christian Podcasts View the latest post Replies 4 Views 28279
- by Eli 1 week ago Python Packages for Scientific Computing View the latest post Replies 7 Views 2713
- by Eli 1 week ago Pondering Big Cosmology Questions Through Lectures and Dialogues View the latest post Replies 32 Views 44704
- by Eli 2 weeks ago C++ PROGRAMMING CRASH COURSE View the latest post Replies 4 Views 1288
-
Automate Reports with Python and Pandas, Save the Output to HTML
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5212
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
This code (adopted from here) automates some report for the mock placement data taken from Kaggle. The generated tabular report and chart are saved to HTML. Run this code at programming-and-computing-with-python-r ... forum-6300
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 0 guests