From 560c18550e8e2a1d9354a473e97234325266ca33 Mon Sep 17 00:00:00 2001 From: Neha Dhaliwal Date: Wed, 5 Mar 2025 21:55:16 -0600 Subject: [PATCH] Done Pandas1 --- Problem 1.py | 13 +++++++++++++ Problem 2.py | 13 +++++++++++++ Problem 3.py | 13 +++++++++++++ Problem 4.py | 15 +++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 Problem 1.py create mode 100644 Problem 2.py create mode 100644 Problem 3.py create mode 100644 Problem 4.py diff --git a/Problem 1.py b/Problem 1.py new file mode 100644 index 0000000..2770eb6 --- /dev/null +++ b/Problem 1.py @@ -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]]) \ No newline at end of file diff --git a/Problem 2.py b/Problem 2.py new file mode 100644 index 0000000..3d49093 --- /dev/null +++ b/Problem 2.py @@ -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) \ No newline at end of file diff --git a/Problem 3.py b/Problem 3.py new file mode 100644 index 0000000..3c828f8 --- /dev/null +++ b/Problem 3.py @@ -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) \ No newline at end of file diff --git a/Problem 4.py b/Problem 4.py new file mode 100644 index 0000000..973c418 --- /dev/null +++ b/Problem 4.py @@ -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) \ No newline at end of file