Informatics Practices
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.
Answer
The DataFrame project is created as follows:
import pandas as pd
data = {'Name': ['Rekha', 'Divya', 'Geet', 'Jeet'],
'Class': ['XII', 'XII', 'XII', 'XII'],
'Section': ['B', 'C', 'H', 'B'],
'Project Name': ['Data Analysis', 'Graphical Analysis', 'Machine Learning', 'App Development']
}
Project = pd.DataFrame(data, index = [101, 102, 103, 104])
print(Project)
Output
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)
Project.loc[105] = [105, 'Arya', 'XI', 'D', 'Web Development']
Project.loc[105] = ['Arya', 'XI', 'D', 'Web Development']
Project.at[106, 'Name'] = 'Vikram'
Project.at[106, 'Class'] = 'XI'
Project.at[106, 'Section'] = 'A'
Project.at[106, 'Project Name'] = 'AI Research'
Output
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
105 Arya XI D Web Development
106 Vikram XI A AI Research
(b)
Project['Grade'] = ['A', 'B+', 'C+', 'B', 'A+', 'C']
Output
Name Class Section Project Name Grade
101 Rekha XII B Data Analysis A
102 Divya XII C Graphical Analysis B+
103 Geet XII H Machine Learning C+
104 Jeet XII B App Development B
105 Arya XI D Web Development A+
106 Vikram XI A AI Research C
(c)
print(Project[['Name', 'Section']])
Output
Name Section
101 Rekha B
102 Divya C
103 Geet H
104 Jeet B
105 Arya D
106 Vikram A
(d)
print(Project.loc[[101, 102]])
Output
Name Class Section Project Name Grade
101 Rekha XII B Data Analysis A
102 Divya XII C Graphical Analysis B+
(e)
Project.insert(1, 'School', ['ABC', 'PQR', 'ABC', 'PQR', 'XYZ', 'XYZ'])
Output
Name School Class Section Project Name Grade
101 Rekha ABC XII B Data Analysis A
102 Divya PQR XII C Graphical Analysis B+
103 Geet ABC XII H Machine Learning c+
104 Jeet PQR XII B App Development B
105 Arya XYZ XI D Web Development A+
106 Vikram XYZ XI A AI Research C
(f)
print(Project.iloc[1:3])
Output
Name School Class Section Project Name Grade
102 Divya PQR XII C Graphical Analysis B+
103 Geet ABC XII H Machine Learning c+
(g)
Project.Class[104] = 'XI'
Project.Section[104] = 'A'
Output
Name School Class Section Project Name Grade
101 Rekha ABC XII B Data Analysis A
102 Divya PQR XII C Graphical Analysis B+
103 Geet ABC XII H Machine Learning c+
104 Jeet PQR XI A App Development B
105 Arya XYZ XI D Web Development A+
106 Vikram XYZ XI A AI Research C
(h)
Project = Project.drop(['Project Name', 'Section'], axis = 1)
Output
Name School Class Grade
101 Rekha ABC XII A
102 Divya PQR XII B+
103 Geet ABC XII c+
104 Jeet PQR XI B
105 Arya XYZ XI A+
106 Vikram XYZ XI C
Related Questions
By default, read_csv() uses the values of first row as column headers in DataFrames. Which argument will you give to ensure that the top/first row's data is used as data and not as column headers ?
Which argument would you give to read_csv() if you only want to read top 10 rows of data ?
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.
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.