ASSIGNMENT4

pdf

School

Northeastern University *

*We aren’t endorsed by this school

Course

6700

Subject

Industrial Engineering

Date

Dec 6, 2023

Type

pdf

Pages

3

Uploaded by HighnessIron12089

Report
PROBLEM 1 Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 35 Los Angeles 3 David 28 Chicago PROBLEM 2 Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 35 Los Angeles 0 Alice 1 Bob 2 Charlie 3 David Name: Name, dtype: object Age of Charlie: 35 Information of the oldest person: Name Age City 2 Charlie 35 Los Angeles Names of people who live in San Francisco: ['Bob'] PROBLEM 3 In [52]: import pandas as pd data = { 'Name' : [ 'Alice' , 'Bob' , 'Charlie' , 'David' ], 'Age' : [ 25 , 30 , 35 , 28 ], 'City' : [ 'New York' , 'San Francisco' , 'Los Angeles' , 'Chicago' ] } df = pd . DataFrame ( data ) print ( df ) In [53]: #q1 print ( df . head ( 3 )) In [54]: #q2 print ( df . Name ) In [55]: #q3 age_of_charlie = df . loc [ df [ 'Name' ] == 'Charlie' , 'Age' ] . values [ 0 ] print ( "Age of Charlie:" , age_of_charlie ) In [56]: #q4 info_of_oldest_person = df [ df [ 'Age' ] == df [ 'Age' ] . max ()] print ( "Information of the oldest person:\n" , info_of_oldest_person ) In [57]: #q5 people_living_in_sf = df [ df [ 'City' ] == 'San Francisco' ][ 'Name' ] . tolist () print ( "Names of people who live in San Francisco:" , people_living_in_sf ) In [58]: #q1 import pandas as pd
Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 35 Los Angeles 3 David 28 Chicago Total sales per category is : Category Clothing 70 Electronics 2300 Name: Price, dtype: int64 Average prices of thr products are : Product Jeans 50.0 Laptop 1200.0 Smartphone 800.0 T-Shirt 20.0 Tablet 300.0 Name: Price, dtype: float64 total revenue generated through sales is 5500 Product with highest price is: Laptop Product with lowest price is: T-Shirt PROBLEM 4 sales_data = { 'Category' : [ 'Electronics' , 'Clothing' , 'Electronics' , 'Clothing' , 'Elec 'Product' : [ 'Laptop' , 'T-Shirt' , 'Smartphone' , 'Jeans' , 'Tablet' ], 'Price' : [ 1200 , 20 , 800 , 50 , 300 ], 'Quantity' : [ 3 , 5 , 2 , 4 , 1 ]} sales_df = pd . DataFrame ( sales_data ) print ( df ) In [59]: #q2 grouped = sales_df . groupby ( 'Category' ) total_sales = grouped [ 'Price' ] . sum () print ( 'Total sales per category is :' , total_sales ) In [60]: #q3 grouped = sales_df . groupby ( 'Product' ) avg_pp = grouped [ 'Price' ] . mean () print ( 'Average prices of thr products are :' , avg_pp ) In [61]: #q4 df [ 'Total Revenue' ] = sales_df [ 'Price' ] * sales_df [ 'Quantity' ] total_revenue = df [ 'Total Revenue' ] . sum () print ( 'total revenue generated through sales is' , total_revenue ) In [62]: #q5 highest_priced_product = sales_df . loc [ sales_df [ 'Price' ] . idxmax ()][ 'Product' ] highest_price = sales_df [ 'Price' ] . max () lowest_priced_product = sales_df . loc [ sales_df [ 'Price' ] . idxmin ()][ 'Product' ] lowest_price = sales_df [ 'Price' ] . min () print ( "Product with highest price is:" , highest_priced_product ) print ( "Product with lowest price is:" , lowest_priced_product )
details of rows where age is greater than 25: Name Age City Total Revenue 1 Bob 30 San Francisco 100 2 Charlie 35 Los Angeles 1600 3 David 28 Chicago 200 details of rows where city is not New York: Name Age City Total Revenue 1 Bob 30 San Francisco 100 2 Charlie 35 Los Angeles 1600 3 David 28 Chicago 200 Rows where products category is Electronics: Category Product Price Quantity 0 Electronics Laptop 1200 3 2 Electronics Smartphone 800 2 4 Electronics Tablet 300 1 Rows where quantity sold is greater than 2: Category Product Price Quantity 0 Electronics Laptop 1200 3 1 Clothing T-Shirt 20 5 3 Clothing Jeans 50 4 DataFrame created with rows wherein price is between $30 and $500: Category Product Price Quantity 3 Clothing Jeans 50 4 4 Electronics Tablet 300 1 In [63]: #q1 age_greater_than_25 = df [ df [ 'Age' ] > 25 ] print ( "details of rows where age is greater than 25:" ) print ( age_greater_than_25 ) In [64]: #q2 city_is_not_new_york = df [ df [ 'City' ] != 'New York' ] print ( "\n details of rows where city is not New York:" ) print ( city_is_not_new_york ) In [65]: #q3 elec_category = sales_df [ sales_df [ 'Category' ] == 'Electronics' ] print ( "Rows where products category is Electronics:" ) print ( elec_category ) In [66]: #q4 quant_gr_than_2 = sales_df [ sales_df [ 'Quantity' ] > 2 ] print ( "Rows where quantity sold is greater than 2:" ) print ( quant_gr_than_2 ) In [67]: #q5 price_30_to_500 = sales_df [( sales_df [ 'Price' ] >= 30 ) & ( sales_df [ 'Price' ] <= 500 )] print ( "DataFrame created with rows wherein price is between $30 and $500:" ) print ( price_30_to_500 ) In [ ]:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help