users = [[0, "Hero", 0],
[1, "Dunn", 2],
[2, "Sue", 3],
[3, "Chi", 3]]
from typing import Tuple, Sequence, List, Any, Callable, Dict, Iterator
from collections import defaultdict
A few type aliases we'll use later
Row = Dict[str, Any] # A database row
WhereClause = Callable[[Row], bool] # Predicate for a single row
HavingClause = Callable[[List[Row]], bool] # Predicate over multiple rows
class Table:
def init(self, columns: List[str], types: List[type]) -> None:
assert len(columns) == len(types), "# of columns must == # of types"
self.columns = columns # Names of columns
self.types = types # Data types of columns
self.rows: List[Row] = [] # (no data yet)
def col2type(self, col: str) -> type:
idx = self.columns.index(col) # Find the index of the column,
return self.types[idx] # and return its type.
......
# Check for right types of values
for value, typ3 in zip(values, self.types):
if not isinstance(value, typ3) and value is not None:
raise TypeError(f"Expected type {typ3} but got {value}")
# Add the corresponding dict as a "row"
self.rows.append(dict(zip(self.columns, values)))
def __getitem__(self, idx: int) -> Row:
return self.rows[idx]
def __iter__(self) -> Iterator[Row]:
return iter(self.rows)
def __len__(self) -> int:
return len(self.rows)
......
return f"{self.columns}\n{rows}"
def update(self,
updates: Dict[str, Any],
predicate: WhereClause = lambda row: True):
# First make sure the updates have valid names and types
for column, new_value in updates.items():
if column not in self.columns:
raise ValueError(f"invalid column: {column}")
.....
# Now update
for row in self.rows:
if predicate(row):
for column, new_value in updates.items():
row[column] = new_value
def delete(self, predicate: WhereClause = lambda row: True) -> None:
"""Delete all rows matching predicate"""
self.rows = [row for row in self.rows if not predicate(row)]
.....
if keep_columns is None: # If no columns specified,
keep_columns = self.columns # return all columns
if additional_columns is None:
additional_columns = {}
# New column names and types
new_columns = keep_columns + list(additional_columns.keys())
keep_types = [self.col2type(col) for col in keep_columns]
# This is how to get the return type from a type annotation.
# It will crash if `calculation` doesn't have a return type.
add_types = [calculation.__annotations__['return']
for calculation in additional_columns.values()]
.....
for row in self.rows:
new_row = [row[column] for column in keep_columns]
for column_name, calculation in additional_columns.items():
new_row.append(calculation(row))
new_table.insert(new_row)
It's a short description of the data science program, For more details about learning the data science and want to complete the data science course and certification then contact us and visit Intellipaat today
Top comments (2)
Data Science Course in Hyderabad
Data Science Training in Hyderabad
Data Science Course Training in Hyderabad
Advanced-Data science training with Free Internship & 100% Placement Assistance
About the Data Science course Training in Hyderabad
Data is everywhere, which is growing exponentially globally, and this may still grow at an accelerating rate for the foreseeable future. Businesses generate massive amounts of data within the type of blogs, messages, transaction documents, mobile device data, social media, etc. By using this data effectively, a firm can create vital value and grow their economy by enhancing productivity, increasing efficiency, and delivering more value to consumers.
Data Science helps in combining the disruption into categories and communicating their potential, which allows data and analytics leaders to drive better results. Top businesses thought there is a necessity to research the data for significant benefits. They use the insights from data for the advantage of users.
Human deciding is becoming increasingly inadequate to pander to a never-ending expansion of the data . However, Data Science and Machine Learning are excelling in solving highly complex data-rich problems. To think beyond the human brain and maintain the balance with the knowledge that's evolved, disrupted, and being employed the sectors altogether, data scientists foster new methodologies. Data scientists must try 'big data expeditions' to explore the data for previously undiscovered value - the first common application of data science. Typical applications include marketing segmentation, advertising, tweaking dynamic pricing models, or banks finding risks and adjusting the financial risk models.
What are the Tools utilized in Data Science?
Nice article, thanks for sharing this information