Data Handling using Pandas — Question 33
Back to all questionsConsider the following dataframe: CORONA and answer the questions given below:
| ID | State | Cases |
|---|---|---|
| 100 | Delhi | 3000 |
| 110 | Mumbai | 4000 |
| 120 | Chennai | 5000 |
| 130 | Surat | 4500 |
Create the above-given dictionary with the given indexes.
(a) Write code to add a new column “Recovery” using the series method to store the number of patients recovered in every state.
(b) To add a new column “Deaths” using the assign() method to store the number of deaths in every state.
(c) To add a new row to store details of another state using loc (assume values).
(d) To add a new column "Percentage" using the insert() method to store the percentage of recovery in every state (assume values). The column should be added as the fourth column in the dataframe.
(e) To delete the column “Percentage” using del command.
(f) To delete the column “Deaths” using pop() method.
(g) To insert a new row of values using iloc[] at the 1st position.
(h) To delete Cases and State temporarily from the dataframe.
The DataFrame CORONA is created as :
import pandas as pd
data = {'State': ['Delhi', 'Mumbai', 'Chennai', 'Surat'],
'Cases': [3000, 4000, 5000, 4500]}
CORONA = pd.DataFrame(data, index=[100, 110, 120, 130])
print(CORONA) State Cases
100 Delhi 3000
110 Mumbai 4000
120 Chennai 5000
130 Surat 4500
(a)
CORONA['Recovery'] = pd.Series([2500, 3000, 3500, 3200], index=[100, 110, 120, 130]) State Cases Recovery
100 Delhi 3000 2500
110 Mumbai 4000 3000
120 Chennai 5000 3500
130 Surat 4500 3200
(b)
CORONA = CORONA.assign(Deaths=[200, 250, 300, 220]) State Cases Recovery Deaths
100 Delhi 3000 2500 200
110 Mumbai 4000 3000 250
120 Chennai 5000 3500 300
130 Surat 4500 3200 220
(c)
CORONA.loc[140] = ['Karnataka', 4200, 2800, 180] State Cases Recovery Deaths
100 Delhi 3000 2500 200
110 Mumbai 4000 3000 250
120 Chennai 5000 3500 300
130 Surat 4500 3200 220
140 Karnataka 4200 2800 180
(d)
CORONA.insert(3, 'Percentage', [80, 75, 70, 71, 67]) State Cases Recovery Percentage Deaths
100 Delhi 3000 2500 80 200
110 Mumbai 4000 3000 75 250
120 Chennai 5000 3500 70 300
130 Surat 4500 3200 71 220
140 Karnataka 4200 2800 67 180
(e)
del CORONA['Percentage'] State Cases Recovery Deaths
100 Delhi 3000 2500 200
110 Mumbai 4000 3000 250
120 Chennai 5000 3500 300
130 Surat 4500 3200 220
140 Karnataka 4200 2800 180
(f)
CORONA.pop('Deaths') State Cases Recovery
100 Delhi 3000 2500
110 Mumbai 4000 3000
120 Chennai 5000 3500
130 Surat 4500 3200
140 Karnataka 4200 2800
(g) The iloc method is not used to add rows to a DataFrame. It is used for index-based or integer-location-based accessing of rows and columns, not for adding rows.
One way of adding a row at a specific position in a DataFrame is by creating a new DataFrame including the row and then concatenating the two DataFrames as shown below :
new_row = {'State': 'Hyderabad', 'Cases': 5200, 'Recovery': 3800}
new_df = pd.DataFrame([new_row], index= [150])
CORONA = pd.concat([new_df, CORONA]) State Cases Recovery
150 Hyderabad 5200 3800
100 Delhi 3000 2500
110 Mumbai 4000 3000
120 Chennai 5000 3500
130 Surat 4500 3200
140 Karnataka 4200 2800
(h)
CORONA.drop(['Cases', 'State'], axis=1, inplace=True) Recovery
100 3800
110 2500
120 3000
130 3500
140 3200
5 2800