Store Year Total_Sales Total_profit 0 S1 2016 150000 15000 1 S4 2016 400000 40000 2 S3 2016 450000 45000 3 S1 2017 350000 35000 4 S2 2016 120000 12000 5 S4 2017 450000 45000 6 S3 2017 500000 50000
In the DataFrame 'df' given above, there is a need for maximum sale for store 'S1' in any year. Which query will be appropriate for the problem?
(A) max_sales = df[df.Store == 'S1']
max_sales['Total_Sales'].max()
(B) max_sales = df[df['Store'] == 'S1']
max_sales['Total_Sales'].max()
(C) max_sales = df[df.Store == 'S1']
max_sales.df['Total_Sales'].max()
(D) max_sales = df[df['Store'] == 'S1']
max_sales.df['Total_Sales'].max()
| Store | Year | Total_Sales | Total_profit | |
|---|---|---|---|---|
| 0 | S1 | 2016 | 150000 | 15000 |
| 1 | S4 | 2016 | 400000 | 40000 |
| 2 | S3 | 2016 | 450000 | 45000 |
| 3 | S1 | 2017 | 350000 | 35000 |
| 4 | S2 | 2016 | 120000 | 12000 |
| 5 | S4 | 2017 | 450000 | 45000 |
| 6 | S3 | 2017 | 500000 | 50000 |
In the DataFrame 'df' given above, there is a need for maximum sale for store 'S1' in any year. Which query will be appropriate for the problem?
(A) max_sales = df[df.Store == 'S1']
max_sales['Total_Sales'].max()
(B) max_sales = df[df['Store'] == 'S1']
max_sales['Total_Sales'].max()
(C) max_sales = df[df.Store == 'S1']
max_sales.df['Total_Sales'].max()
(D) max_sales = df[df['Store'] == 'S1']
max_sales.df['Total_Sales'].max()
Solution
Filtering with a Boolean condition, df[df.Store == 'S1'] or df[df['Store'] == 'S1'], keeps only the rows for store S1. Taking ['Total_Sales'].max() on that filtered DataFrame then gives the highest sales for S1, which is .
Both (A) and (B) do this. Dot notation (df.Store) and bracket notation (df['Store']) select the same column.
(C) and (D): max_sales is already a DataFrame, so max_sales.df is not a valid attribute and raises an AttributeError.
Related questions:
2025: 22 May Shift 1
2025: 13 May Shift 2
2025: 30 May Shift 2