Assignment 4
.pdf
keyboard_arrow_up
School
Northeastern University *
*We aren’t endorsed by this school
Course
6400
Subject
Information Systems
Date
Apr 3, 2024
Type
Pages
5
Uploaded by ColonelMosquitoMaster1017
In [1]:
Question 1:
In [5]:
In [6]:
Question 2
In [23]:
In [28]:
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 22 Los Angeles
3 David 35 Chicago
4 Eve 28 Miami
Out[23]:
Name
Age
City
0
Alice
25
New York
1
Bob
30
San Francisco
2
Charlie
22
Los Angeles
Out[28]:
Name
0
Alice
1
Bob
2
Charlie
3
David
4
Eve
import
pandas
as
pd
# 1
data
=
{ 'Name'
: [
'Alice'
, 'Bob'
, 'Charlie'
, 'David'
, 'Eve'
],
'Age'
: [
25
, 30
, 22
, 35
, 28
],
'City'
: [
'New York'
, 'San Francisco'
, 'Los Angeles'
, 'Chicago
df
=
pd.DataFrame(data)
# 2
print
(df)
# 1
df.iloc[:
3
,]
# 2
df[[
"Name"
]]
Assignment 4 - Jupyter Notebook
http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng...
1 of 5
9/29/23, 8:42 PM
In [32]:
In [39]:
In [38]:
Question 3
In [55]:
Out[32]:
Age
2
22
Out[39]:
Name David
Age 35
City Chicago
Name: 3, dtype: object
Out[38]:
Name
1
Bob
Out[55]:
Category
Product
Price
Quantity
0
Electronics
Laptop
1200
3
1
Clothing
T-Shirt
20
5
2
Electronics
Smartphone
800
2
3
Clothing
Jeans
50
4
4
Electronics
Tablet
300
1
# 3
df.loc[df.Name
==
"Charlie"
,[
"Age"
]]
# 4
df.loc[df[
"Age"
].idxmax()]
# 5
df.loc[df[
"City"
]
==
"San Francisco"
,[
"Name"
]]
# 1
sales_data
=
{ 'Category'
: [
'Electronics'
, 'Clothing'
, 'Electronics'
, 'Product'
: [
'Laptop'
, 'T-Shirt'
, 'Smartphone'
, 'Jeans'
,
'Price'
: [
1200
, 20
, 800
, 50
, 300
],
'Quantity'
: [
3
, 5
, 2
, 4
, 1
]}
sales_df
=
pd.DataFrame(sales_data)
sales_df
Assignment 4 - Jupyter Notebook
http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng...
2 of 5
9/29/23, 8:42 PM
In [60]:
In [87]:
In [63]:
Total Sales for Clothing: 300
Total Sales for Electronics: 5500
Average Price for Jeans: 50.0
Average Price for Laptop: 1200.0
Average Price for Smartphone: 800.0
Average Price for T-Shirt: 20.0
Average Price for Tablet: 300.0
Total Sales: 5800
# 2
group_category
=
sales_df.groupby(
"Category"
)
groups_dictionary
=
group_category.groups
for
i
in
groups_dictionary:
result
=
group_category.get_group(i)
total_sales
=
0
for
j
in
groups_dictionary[i]:
total_sales
=
total_sales
+
(result.loc[j,
"Price"
]
*
result.loc[
print
(
"Total Sales for {}: {}"
.format(i,total_sales))
# 3
group_product
=
sales_df.groupby(
"Product"
)
groups_product
=
group_product.groups
for
i
in
groups_product:
result
=
group_product.get_group(i)
average_price
=
0
for
j
in
groups_product[i]:
average_price
=
average_price
+
(result.loc[j,
"Price"
])
average_price
=
average_price
/
(
len
(result))
print
(
"Average Price for {}: {}"
.format(i,average_price))
# 4
total_sales
=
0
for
i
in
range
(
len
(sales_df)):
total_sales
=
total_sales
+
(sales_df.loc[i,
"Price"
]
*
sales_df.loc[
print
(
"Total Sales: {}"
.format(total_sales))
Assignment 4 - Jupyter Notebook
http://localhost:8888/notebooks/Documents/Data%20Analytics%20Eng...
3 of 5
9/29/23, 8:42 PM
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