Informatics Practices
Consider 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.
Python Data Handling
2 Likes
Answer
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)
Output
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])
Output
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])
Output
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]
Output
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])
Output
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']
Output
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')
Output
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])
Output
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)
Output
Recovery
100 3800
110 2500
120 3000
130 3500
140 3200
5 2800
Answered By
2 Likes
Related Questions
Which argument would you give to read_csv() if you only want to read top 10 rows of data ?
Create the following dataframe by the name Project regarding a competition and answer the questions given below:
Enrolment No. Name Class Section Project Name 101 Rekha XII B Data Analysis 102 Divya XII C Graphical Analysis 103 Geet XII H Machine Learning 104 Jeet XII B App Development (a) Insert two records with different methods.
(b) Insert a column to store grades given to their projects.
(c) Write a command to display the name and section for all.
(d) Write a command to display the records with index value 101 and 102.
(e) Insert a column after name to store the school name.
(f) Display the second and third record.
(g) Replace the name and section of Jeet to 'XI','A'.
(h) Remove the column Project Name and Section.
Create a dataframe ‘Student’ from two series—Name and Grade, Name and Marks of five students.
(a) Display the first three records from student dataframe.
(b) Display the last two records from student dataframe.
Create a dataframe of dictionary consisting of Name, Sub1, Sub2, Sub3, Sub4, Sub5 of five students.
(a) Display the dataframe.
(b) Display the first 5 rows and bottom 3 rows of student dataframe.