Page 101 - Informatics_Practices_Fliipbook_Class12
P. 101

19.  Assuming the given table: Product. Write the python code for the following:
                       Item     Company         Rupees         USD
                        TV      LG              12000          700
                        TV      VIDEOCON        10000          650
                        TV      LG              15000          800
                        AC      SONY            14000          750
                  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.
             Ans.  a. To create the data frame for the above table.

                    import pandas as pd
                    myDict = {'Item':['TV','TV','TV','AC'],'Company':['LG','VIDEOCON','LG','SONY'],
                           'Rupees':[12000,10000,15000,14000],'USD':[700,600,800,750]}
                    df = pd.DataFrame(myDict)
                    print(df)
                  b.  To add the new rows in the data frame.
                    newRows = {'Item': ['Fridge', 'Washing Machine'],
                                'Company': ['Whirlpool', 'Samsung'],
                                'Rupees': [18000, 20000],
                                'USD': [1000, 1200]}
                    df = df.append(pd.DataFrame(newRows), ignore_index=True)
                    print("DataFrame after adding new rows:")
                    print(df)
                    print("\n")
                  c.  To display the maximum price of LG TV.
                    maxPriceLgTv = df[(df['item'] == 'TV') & (df['company'] == 'LG')]['rupees'].max()
                    print("Maximum price of LG TV:", maxPriceLgTv)
                    print("\n")
                  d.  To display the Sum of all products.


                    totalSum = df['rupees'].sum()
                    print("Sum of all products:", totalSum)
                    print("\n")
                  e.  To display the median of the USD of Sony products.

                    medianUsdSony = df[df['company'] == 'SONY']['usd'].median()
                    print("Median of USD for Sony products:", medianUsdSony)
                    print("\n")
                  f.  To sort the data according to the Rupees and transfer the data to MySQL.
                     The following code sorts the DataFrame by the 'Rupees' column, establishes a connection to a MySQL database, and
                    then transfers the sorted DataFrame to a table named 'Product_table' within that database.
                     import pymysql
                    print(df.sort_values(by=['Rupees']))


                                                                             Data Handling using Pandas DataFrame  87
   96   97   98   99   100   101   102   103   104   105   106