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.
# 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
Post a Comment