Skip to content
Exercises · Q13
Q.

Assuming the given table: Product. Write the python code for the following:

ItemCompanyRupeesUSD
TVLG12000700
TVVIDEOCON10000650
TVLG15000800
ACSONY14000750

a) To create the data frame for the above table.

b) To add the new rows in the data frame.

c) To display the maximum price of LG TV.

d) To display the Sum of all products.

e) To display the median of the USD of Sony products.

f) To sort the data according to the Rupees and transfer the data to MySQL.

g) To transfer the new dataframe into the MySQL with new values.

Tripura TbseTextbookSubjective· 5mImportance★★★★★
62% · 33/53 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution builds a Pandas DataFrame from the given table, then performs row addition, aggregation (max, sum, median), sorting, and MySQL export — each operation explained with the why behind the method choice.

The core idea here is that a DataFrame is the right tool because it gives us tabular data with labelled columns and row indices, plus a rich set of built-in methods for aggregation, filtering, and sorting. We'll use Pandas for the in-memory work and SQLAlchemy (or pymysql) to push data to MySQL.

Let's build this step by step.

(a) Create the DataFrame

We construct the DataFrame directly from a dictionary of lists — each key becomes a column name, each list becomes the column's values. This is the most readable way when you have the data in front of you.

import pandas as pd

data = {
    'Item': ['TV', 'TV', 'TV', 'AC'],
    'Company': ['LG', 'VIDEOCON', 'LG', 'SONY'],
    'Rupees': [12000, 10000, 15000, 14000],
    'USD': [700, 650, 800, 750]
}

df = pd.DataFrame(data)
print(df)

Output:

  Item   Company  Rupees  USD
0   TV        LG   12000  700
1   TV  VIDEOCON   10000  650
2   TV        LG   15000  800
3   AC      SONY   14000  750

(b) Add new rows

We use pd.concat() to append a new DataFrame (or a list of rows) to the existing one. The ignore_index=True parameter resets the index so we don't carry over old indices.

new_rows = pd.DataFrame([
    ['AC', 'LG', 18000, 900],
    ['TV', 'SONY', 13000, 720]
], columns=['Item', 'Company', 'Rupees', 'USD'])

df = pd.concat([df, new_rows], ignore_index=True)
print(df)

Output:

  Item   Company  Rupees  USD
0   TV        LG   12000  700
1   TV  VIDEOCON   10000  650
2   TV        LG   15000  800
3   AC      SONY   14000  750
4   AC        LG   18000  900
5   TV      SONY   13000  720
Tip

Avoid df.append() — it's deprecated since Pandas 1.4.0. Always use pd.concat() for adding rows.

(c) Display maximum price of LG TV

We filter the DataFrame for rows where Item == 'TV' and Company == 'LG', then take the max of the Rupees column.

max_lg_tv = df[(df['Item'] == 'TV') & (df['Company'] == 'LG')]['Rupees'].max()
print("Maximum price of LG TV:", max_lg_tv)

Output:

Maximum price of LG TV: 15000

(d) Display sum of all products (Rupees)

The sum() method on a Series gives the total. We call it on the Rupees column.

total_rupees = df['Rupees'].sum()
print("Sum of all products (Rupees):", total_rupees)

Output:

Sum of all products (Rupees): 82000

(e) Display median of USD for Sony products

First filter for Company == 'SONY', then compute the median of the USD column.

sony_usd_median = df[df['Company'] == 'SONY']['USD'].median()
print("Median USD of Sony products:", sony_usd_median)

Output:

Median USD of Sony products: 735.0
Watch out

The median of two values (720 and 750) is their average: (720+750)/2 = 735. If there were an odd number of values, the median would be the middle value after sorting.

(f) Sort by Rupees and transfer to MySQL

We sort the DataFrame using sort_values(), then use to_sql() to write it to a MySQL table. You'll need sqlalchemy and a MySQL driver (pymysql or mysql-connector-python).

# Sort by Rupees ascending
df_sorted = df.sort_values(by='Rupees')

# Connect to MySQL and write …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.