From dc6b906a182e8e6a5745de9b0a178b462dc62d37 Mon Sep 17 00:00:00 2001 From: Kaaviya Varrshini Date: Sat, 14 Jun 2025 19:52:49 -0400 Subject: [PATCH] Done Pandas1 --- Problem1_2DList.py | 15 +++++++++++++++ Problem2_BigCountry.py | 5 +++++ Problem3_Recyclable.py | 5 +++++ Problem4_CustomerNoOrder.py | 15 +++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 Problem1_2DList.py create mode 100644 Problem2_BigCountry.py create mode 100644 Problem3_Recyclable.py create mode 100644 Problem4_CustomerNoOrder.py diff --git a/Problem1_2DList.py b/Problem1_2DList.py new file mode 100644 index 0000000..27018d0 --- /dev/null +++ b/Problem1_2DList.py @@ -0,0 +1,15 @@ +import pandas as pd + +# Two-dimensional list +data = [['Geek1', 26, 'Scientist'], + ['Geek2', 31, 'Researcher'], + ['Geek3', 24, 'Engineer']] + +# Column names +columns = ['Name', 'Age', 'Occupation'] + +# Creating DataFrame using pd.DataFrame.from_dict() +df = pd.DataFrame.from_dict(dict(zip(columns, zip(*data)))) + +# Displaying the DataFrame +print(df) \ No newline at end of file diff --git a/Problem2_BigCountry.py b/Problem2_BigCountry.py new file mode 100644 index 0000000..67b9411 --- /dev/null +++ b/Problem2_BigCountry.py @@ -0,0 +1,5 @@ +import pandas as pd + +def big_countries(world: pd.DataFrame) -> pd.DataFrame: + df=world[(world['area']>=3000000) | (world['population']>=25000000)] + return pd.DataFrame(df[['name','population','area']]) \ No newline at end of file diff --git a/Problem3_Recyclable.py b/Problem3_Recyclable.py new file mode 100644 index 0000000..60cd155 --- /dev/null +++ b/Problem3_Recyclable.py @@ -0,0 +1,5 @@ +import pandas as pd + +def find_products(products: pd.DataFrame) -> pd.DataFrame: + df=products[(products['low_fats']=='Y') & (products['recyclable']=='Y')] + return pd.DataFrame(df[['product_id']]) \ No newline at end of file diff --git a/Problem4_CustomerNoOrder.py b/Problem4_CustomerNoOrder.py new file mode 100644 index 0000000..c24ce5d --- /dev/null +++ b/Problem4_CustomerNoOrder.py @@ -0,0 +1,15 @@ +import pandas as pd + +#Soln 1: Using Merge +def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame: + df=customers.merge(orders, + left_on ='id', + right_on='customerId', + how='left') + df=df[df['id_y'].isnull()] + print(df[['name']]) + return pd.DataFrame(df[['name']]).rename(columns={'name':'Customers'}) + +#Soln 2: Using isin() + df=customers[~customers['id'].isin(orders['customerId'])] + return pd.DataFrame(df[['name']]).rename(columns={'name':'Customers'}) \ No newline at end of file