import numpy as np
from IPython.display import display, HTMLRecreating the DataFrame class
class DataFrameCopy:
def __init__(self, data: dict):
self.data = data
self.create_index() # Run method to create index on initializing of object
# Add index to dataset
def create_index(self):
df = {}
max_rows = 0
for key, value in self.data.items(): # return max number of rows to propagate index
if len(value) > max_rows:
max_rows = len(value)
index_length = list(range(0, max_rows)) # Create index column
df['index'] = index_length # Add index to temp dict
df.update(self.data) # Add initialize data to temp dict and reassign data that includes index
self.data = df
# Replicating the head function of pandas
def head(self, rows: int):
table = '<table style="border-collapse: collapse; font-family: Arial; font-size: 14px;">'
# loop to insert columns
table += '<tr>'
for key in self.data.keys():
table += f'<th>{key}</th>'
table += '</tr>'
# Below is the loop to insert rows values for each column
i = 0 # i is used to keep track of the row level, whereby
while True:
for key, value in self.data.items():
table += f"<td>{value[i]}</td>"
table += "</tr>"
i += 1
# When desired amount of rows have been inserted in html, break loop
if i == rows:
break
table += "</table>"
# Read string as html
chart = HTML(table)
return display(chart)
Initializing DataFrameCopy class with data in the form of dictionary just like in pandas
data = {'Col 1': [1, 2, 3, 4, 5], 'Col 2': [6, 7, 8, 9, 10]}
df = DataFrameCopy(data)Below the data from the object is called where we can see that the index has been automatically created
df.data{'index': [0, 1, 2, 3, 4], 'Col 1': [1, 2, 3, 4, 5], 'Col 2': [6, 7, 8, 9, 10]}
Using the head method for the class works simply like the pandas version, where we can choose the amount of rows we would like to show. The method then displays a html table of the data for improved readability.
df.head(4)| index | Col 1 | Col 2 |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |