Skip to main content

DATAFRAME IMP QUESTIONS

 


QUE. Consider the following CORONA DataFrame and answer the questions given below:

ID

State

Cases

100

Delhi

3000

110

Mumbai

4000

120

Chennai

5000

130

Surat

4500

Create the above dictionary and DataFrame with given data and perform the following operations:

(a) Write code to add a new column named 'Recovery' using the Series method. This column should store the number of patients recovered in each state. (Assume appropriate values)

(b) Add a new column named 'Deaths' using the assign() method to store the number of deaths in each state. (Assume values)

(c) Add a new row using loc[] to store details of another state. (Assume values)

(d) Add a new column named 'Percentage' using the insert() method. This column should store the percentage of recovery in each state and must be added as the fourth column of the DataFrame. (Assume values)

(e) Delete the column 'Percentage' using the del command.

(f) Delete the column 'Deaths' using the pop() method.

(g) Insert a new row using iloc[] method at the first position (i.e., index 0). (Assume values)

(h) Temporarily delete the columns 'Cases' and 'State' from the DataFrame without modifying the original.

 Sol:

  import pandas as pd

# Creating the dictionary

data = {

    'ID': [100, 110, 120, 130],

    'State': ['Delhi', 'Mumbai', 'Chennai', 'Surat'],

    'Cases': [3000, 4000, 5000, 4500]

}

CORONA = pd.DataFrame(data)

 (a) Add a new column 'Recovery' using the Series method

recovery = pd.Series([2800, 3700, 4700, 4200])

CORONA['Recovery'] = recovery

(b) Add a new column 'Deaths' using the assign() method

CORONA['Deaths'] = [150, 180, 200, 250, 190]

(c) Add a new row using loc[] (e.g., for Bangalore)

CORONA.loc[4] = [140, 'Bangalore', 4800, 4600, 190]

(d) Add a column 'Percentage' using the insert() method (at index 3)

percentage = [93.3, 92.5, 94.0, 93.3, 95.8]

CORONA.insert(3, 'Percentage', percentage)

(e) Delete the column 'Percentage' using del

del CORONA['Percentage']

(f) Delete the column 'Deaths' using pop() method

CORONA.pop('Deaths')

(g) Insert a new row using iloc[] at the 1st position

new_row = [150, 'Hyderabad', 4200, 4000]

# Insert at position 0

CORONA.loc[-1] = new_row        # Add the new row with a temporary index

CORONA.index = CORONA.index + 1 # Shift all indexes by 1

CORONA = CORONA.sort_index()    # Sort by index to reorder

(h) Temporarily delete 'Cases' and 'State' (without modifying original)

temp_df = CORONA.drop(['Cases', 'State'], axis=1)

print(temp_df)


QUE 2:

Consider the following EMPLOYEE DataFrame and answer the questions given below:

EmpID

Name

Department

Salary

101

Amit

HR

40000

102

Neha

IT

55000

103

Raj

Finance

50000

104

Priya

IT

60000

Create the above dictionary and DataFrame with given data and perform the following operations:

(a) Add a new column named Bonus using the Series method. (Assume values for each employee)

(b) Add a new column named Tax using the assign() method. (Assume values for each employee)

(c) Add a new row using loc[] to store details of another employee. (Assume values)

(d) Delete the column NetSalary using the del command.

(e) Delete the column Tax using the pop() method.

 Sol:

import pandas as pd

# Creating the dictionary

data = {

    'EmpID': [101, 102, 103, 104],

    'Name': ['Amit', 'Neha', 'Raj', 'Priya'],

    'Department': ['HR', 'IT', 'Finance', 'IT'],

    'Salary': [40000, 55000, 50000, 60000]

}

EMPLOYEE = pd.DataFrame(data)

print("Initial DataFrame:\n", EMPLOYEE)

 

# (a) Add a new column 'Bonus' using the Series method

bonus = pd.Series([5000, 7000, 6500, 8000])

EMPLOYEE['Bonus'] = bonus

print("\nAfter adding Bonus column:\n", EMPLOYEE)

 

# (b) Add a new column 'Tax' using assign() method

EMPLOYEE = EMPLOYEE.assign(Tax=[4000, 5500, 5000, 6000])

print("\nAfter adding Tax column:\n", EMPLOYEE)

 

# (c) Add a new row using loc[] (for example, for employee Sanya)

EMPLOYEE.loc[4] = [105, 'Sanya', 'HR', 45000, 6000, 4500]

print("\nAfter adding a new row using loc[]:\n", EMPLOYEE)

 

# (d) Delete the column 'NetSalary' using del

del EMPLOYEE['NetSalary']

print("\nAfter deleting NetSalary column:\n", EMPLOYEE)

 

# (e) Delete the column 'Tax' using pop() method

EMPLOYEE.pop('Tax')

print("\nAfter deleting Tax column:\n", EMPLOYEE)

 

 

 


Comments

Popular posts from this blog

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...