- 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 5 hours ago Inaugural Lecture by Prof. Mukandala: Dola, Soko na Kushindwa kwa Taasisi View the latest post Replies 2 Views 55
- by Eli 1 day ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 332 Views 41300
- by Eli 1 day ago Iran's President Ebrahim Raisi Aged 63 Dies in a Helicopter Crash View the latest post Replies 3 Views 66
- by Eli 1 day ago Re: What is in Your Mind? View the latest post Replies 717 Views 308098
- by Eli 3 days ago PySpark for Large Data Processing View the latest post Replies 2 Views 8183
- by Eli 3 days ago Online Bible View the latest post Replies 3 Views 23334
- by Eli 4 days ago Generating SSH Key and Adding it to the ssh-agent for Authentication on GitHub View the latest post Replies 1 Views 514
- by Eli 1 week ago Russia Invades Ukraine View the latest post Replies 663 Views 242004
- by Eli 2 weeks ago President Museveni's Speech During International Development Association (IDA) Summit View the latest post Replies 1 Views 509
- by Eli 2 weeks ago From Simple Linear Regression Analysis to Covariance & Correlation to Independent Determinant, and R-Squared View the latest post Replies 11 Views 25150
-
Automate Reports with Python and Pandas, Save the Output to HTML
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5412
- 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 1 guest