Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Problem 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Make a Pandas DataFrame with two-dimensional list

import pandas as pd

def listToDataframe(twoD_list:list[[]]) -> pd.DataFrame:
cols=[]
for i in range(len(twoD_list[0])):
cols.append(f'cols_{i}')
df = pd.DataFrame(twoD_list, columns=cols)
print(df)
return df

listToDataframe(twoD_list=[['Semester1', 25, 77], ['Semester2', 30, 67], ['Semester3', 26, 90], ['Semester4', 22, 88]])
13 changes: 13 additions & 0 deletions Problem 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Big Countries

import pandas as pd

def big_countries(world: pd.DataFrame) -> pd.DataFrame:
df = world[(world['area']>=3000000) | (world['population']>=25000000)]
df = df[['name', 'population', 'area']]
print(df)
return df

data = [['Afghanistan', 'Asia', 652230, 25500100, 20343000000], ['Albania', 'Europe', 28748, 2831741, 12960000000], ['Algeria', 'Africa', 2381741, 37100000, 188681000000], ['Andorra', 'Europe', 468, 78115, 3712000000], ['Angola', 'Africa', 1246700, 20609294, 100990000000]]
world = pd.DataFrame(data, columns=['name', 'continent', 'area', 'population', 'gdp']).astype({'name':'object', 'continent':'object', 'area':'Int64', 'population':'Int64', 'gdp':'Int64'})
big_countries(world)
13 changes: 13 additions & 0 deletions Problem 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Recyclable and Low Fat Products

import pandas as pd

def find_products(products: pd.DataFrame) -> pd.DataFrame:
df = products[(products['low_fats'] == 'Y') & (products['recyclable'] == 'Y')]
df = df[['product_id']]
print(df)
return df

data = [['0', 'Y', 'N'], ['1', 'Y', 'Y'], ['2', 'N', 'Y'], ['3', 'Y', 'Y'], ['4', 'N', 'N']]
products = pd.DataFrame(data, columns=['product_id', 'low_fats', 'recyclable']).astype({'product_id':'int64', 'low_fats':'category', 'recyclable':'category'})
find_products(products)
15 changes: 15 additions & 0 deletions Problem 4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Customer Who Never Order

import pandas as pd

def find_customers(customers:pd.DataFrame, orders: pd.DataFrame)->pd.DataFrame:
df = customers[~customers['id'].isin(orders['customerId'])]
df = df[['name']].rename(columns = {'name':'Customers'})
print(df)
return df

data = [[1, 'Joe'], [2, 'Henry'], [3, 'Sam'], [4, 'Max']]
customers = pd.DataFrame(data, columns=['id', 'name']).astype({'id':'Int64', 'name':'object'})
data = [[1, 3], [2, 1]]
orders = pd.DataFrame(data, columns=['id', 'customerId']).astype({'id':'Int64', 'customerId':'Int64'})
find_customers(customers,orders)