Short Answer Questions
Adding Rows: Write a Python code snippet to add a new row to the following DataFrame:
import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 24]} df = pd.DataFrame(data)
Add a row with
Name: 'Tom'
andAge: 30
.Adding Columns: Given the following DataFrame:
import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 24]} df = pd.DataFrame(data)
Write a Python code snippet to add a new column
'City'
with values'New York'
and'Boston'
.Insert Column at Specific Position: How can you insert a new column
'Gender'
after the'Name'
column in the DataFrame shown above? Write the code for it.Multiple Rows: Explain how you would add multiple rows at once to a DataFrame. Provide a code example where two rows are added with names
'Alice'
and'Bob'
, and ages22
and26
respectively.
Section C: Programming Task
Create a DataFrame with columns
'StudentID'
,'Name'
, and'Marks'
. Add the following data:- Row 1:
101
,'Alice'
,85
- Row 2:
102
,'Bob'
,90
- Row 3:
103
,'Charlie'
,78
Then, add a new row for a student with
StudentID: 104
,Name: 'David'
, andMarks: 88
.- Row 1:
Add a new column
'Grade'
to the DataFrame created in Task 1. Assign grades based on the following criteria:- Marks >= 85:
'A'
- Marks 70-84:
'B'
- Marks < 70:
'C'
- Marks >= 85:
0 Comments
Please do note create link post in comment section