From de0bbf9e27a3f466b7295c2c994da4fb9534160a Mon Sep 17 00:00:00 2001 From: Feminto Date: Sun, 8 Jun 2025 22:20:46 -0700 Subject: [PATCH] Adding solution for all questions --- BigCountries.py | 5 ++ CustomersWhoNeverOrder.py | 5 ++ ...PandasDataframeWithTwo-DimensionalLists.py | 62 +++++++++++++++++++ RecyclableAndLowFatProducts.py | 5 ++ 4 files changed, 77 insertions(+) create mode 100644 BigCountries.py create mode 100644 CustomersWhoNeverOrder.py create mode 100644 MakeAPandasDataframeWithTwo-DimensionalLists.py create mode 100644 RecyclableAndLowFatProducts.py diff --git a/BigCountries.py b/BigCountries.py new file mode 100644 index 0000000..7c8860e --- /dev/null +++ b/BigCountries.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 df[['name','population','area']] \ No newline at end of file diff --git a/CustomersWhoNeverOrder.py b/CustomersWhoNeverOrder.py new file mode 100644 index 0000000..206a957 --- /dev/null +++ b/CustomersWhoNeverOrder.py @@ -0,0 +1,5 @@ +import pandas as pd + +def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame: + df = customers[~(customers['id'].isin(orders['customerId']))] + return df[['name']].rename(columns = {'name':'customers'}) \ No newline at end of file diff --git a/MakeAPandasDataframeWithTwo-DimensionalLists.py b/MakeAPandasDataframeWithTwo-DimensionalLists.py new file mode 100644 index 0000000..0e93c9c --- /dev/null +++ b/MakeAPandasDataframeWithTwo-DimensionalLists.py @@ -0,0 +1,62 @@ +#Q1 +#Create Pandas Dataframe from 2D List using pd.DataFrame() + +import pandas as pd + +list_1 = [['Geek', 25], + ['is', 30], + ['for', 26], + ['Geeksforgeeks', 22]] + +# creating df object with columns specified +df = pd.DataFrame(list_1, columns =['Tag','number']) +print(df ) + + +#Q2 +#Create Pandas Dataframe from 2D List using pd.DataFrame.from_records() + +import pandas as pd + +data = [['Geek1', 28, 'Analyst'], + ['Geek2', 35, 'Manager'], + ['Geek3', 29, 'Developer']] + +columns = ['Name', 'Age', 'Occupation'] + +# Creating DataFrame using pd.DataFrame.from_records() +df = pd.DataFrame.from_records(data, columns=columns) +print(df) + + +#Q3 +#Create Pandas Dataframe from 2D List using pd.DataFrame.from_dict() + +import pandas as pd + +data = [['Geek1', 26, 'Scientist'], + ['Geek2', 31, 'Researcher'], + ['Geek3', 24, 'Engineer']] + +columns = ['Name', 'Age', 'Occupation'] + +# Creating DataFrame using pd.DataFrame.from_dict() +df = pd.DataFrame.from_dict(dict(zip(columns, zip(*data)))) +print(df) + + + +#Q4 +#Create Pandas Dataframe from 2D List using Specifying Data Types +import pandas as pd + +data = [['Geek1', 'Reacher', 25], + ['Geek2', 'Pete', 30], + ['Geek3', 'Wilson', 26], + ['Geek4', 'Williams', 22]] + +columns = ['FName', 'LName', 'Age'] + +# Creating DataFrame with specified data types +df = pd.DataFrame(data, columns=columns) +print(df) \ No newline at end of file diff --git a/RecyclableAndLowFatProducts.py b/RecyclableAndLowFatProducts.py new file mode 100644 index 0000000..4e658fc --- /dev/null +++ b/RecyclableAndLowFatProducts.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 df[['product_id']] \ No newline at end of file