Python Pandas — I — Question 11
Back to all questionsWrite a program that stores the sales of 5 fast moving items of a store for each month in 12 Series objects, i.e., S1 Series object stores sales of these 5 items in 1st month, S2 stores sales of these 5 items in 2nd month, and so on.
The program should display the summary sales report like this :
Total Yearly Sales, item-wise (should display sum of items' sales over the months)
Maximum sales of item made : <name of item that was sold the maximum in whole year>
Maximum sales for individual items
Maximum sales of item 1 made : <month in which that item sold the maximum>
Maximum sales of item 2 made : <month in which that item sold the maximum>
Maximum sales of item 3 made : <month in which that item sold the maximum>
Maximum sales of item 4 made : <month in which that item sold the maximum>
Maximum sales of item 5 made : <month in which that item sold the maximum>
import pandas as pd
sales_data = {
'Month_1': pd.Series([300, 250, 200, 150, 350], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_2': pd.Series([380, 210, 220, 180, 320], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_3': pd.Series([320, 270, 230, 200, 380], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_4': pd.Series([310, 260, 210, 190, 360], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_5': pd.Series([290, 240, 220, 170, 340], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_6': pd.Series([300, 250, 400, 160, 350], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_7': pd.Series([310, 260, 230, 180, 370], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_8': pd.Series([320, 270, 240, 190, 380], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_9': pd.Series([330, 280, 250, 200, 400], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_10': pd.Series([340, 290, 260, 510, 420], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_11': pd.Series([350, 300, 270, 220, 440], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']),
'Month_12': pd.Series([360, 390, 280, 230, 260], index=['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5'])
}
sales_df = pd.DataFrame(sales_data)
print("Total Yearly Sales, item-wise:")
total_sales = sales_df.sum()
print(total_sales)
t = sales_df.sum(axis=1)
max_sales_item = t.idxmax()
print("\nMaximum sales of item made: ", max_sales_item)
print("\nMaximum sales for individual items:")
for item_num in range(1, 6):
max_sales_month = None
max_sales_value = 0
for month in sales_df.columns:
if sales_df[month][f'Item_{item_num}'] > max_sales_value:
max_sales_value = sales_df[month][f'Item_{item_num}']
max_sales_month = month
print("Maximum sales of item", item_num, "made: ", max_sales_month)Total Yearly Sales, item-wise:
Month_1 1250
Month_2 1310
Month_3 1400
Month_4 1330
Month_5 1260
Month_6 1460
Month_7 1350
Month_8 1400
Month_9 1460
Month_10 1820
Month_11 1580
Month_12 1520
dtype: int64
Maximum sales of item made: Item_5
Maximum sales for individual items:
Maximum sales of item 1 made: Month_2
Maximum sales of item 2 made: Month_12
Maximum sales of item 3 made: Month_6
Maximum sales of item 4 made: Month_10
Maximum sales of item 5 made: Month_11
import
pandas
as
pd
sales_data
=
{
'Month_1'
:
pd
.
Series
([
300
,
250
,
200
,
150
,
350
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_2'
:
pd
.
Series
([
380
,
210
,
220
,
180
,
320
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_3'
:
pd
.
Series
([
320
,
270
,
230
,
200
,
380
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_4'
:
pd
.
Series
([
310
,
260
,
210
,
190
,
360
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_5'
:
pd
.
Series
([
290
,
240
,
220
,
170
,
340
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_6'
:
pd
.
Series
([
300
,
250
,
400
,
160
,
350
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_7'
:
pd
.
Series
([
310
,
260
,
230
,
180
,
370
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_8'
:
pd
.
Series
([
320
,
270
,
240
,
190
,
380
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_9'
:
pd
.
Series
([
330
,
280
,
250
,
200
,
400
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_10'
:
pd
.
Series
([
340
,
290
,
260
,
510
,
420
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_11'
:
pd
.
Series
([
350
,
300
,
270
,
220
,
440
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
]),
'Month_12'
:
pd
.
Series
([
360
,
390
,
280
,
230
,
260
],
index
=
[
'Item_1'
,
'Item_2'
,
'Item_3'
,
'Item_4'
,
'Item_5'
])
}
sales_df
=
pd
.
DataFrame
(
sales_data
)
print
(
"Total Yearly Sales, item-wise:"
)
total_sales
=
sales_df
.
sum
()
print
(
total_sales
)
t
=
sales_df
.
sum
(
axis
=
1
)
max_sales_item
=
t
.
idxmax
()
print
(
"
\n
Maximum sales of item made: "
,
max_sales_item
)
print
(
"
\n
Maximum sales for individual items:"
)
for
item_num
in
range
(
1
,
6
):
max_sales_month
=
None
max_sales_value
=
0
for
month
in
sales_df
.
columns
:
if
sales_df
[
month
][
f'Item_
{
item_num
}
'
]
>
max_sales_value
:
max_sales_value
=
sales_df
[
month
][
f'Item_
{
item_num
}
'
]
max_sales_month
=
month
print
(
"Maximum sales of item"
,
item_num
,
"made: "
,
max_sales_month
)
Output
Total Yearly Sales, item-wise:
Month_1 1250
Month_2 1310
Month_3 1400
Month_4 1330
Month_5 1260
Month_6 1460
Month_7 1350
Month_8 1400
Month_9 1460
Month_10 1820
Month_11 1580
Month_12 1520
dtype: int64
Maximum sales of item made: Item_5
Maximum sales for individual items:
Maximum sales of item 1 made: Month_2
Maximum sales of item 2 made: Month_12
Maximum sales of item 3 made: Month_6
Maximum sales of item 4 made: Month_10
Maximum sales of item 5 made: Month_11