11. How would you add a new column namely ‘val’ to a dataframe df that has 10 rows in it and has columns as ‘Item’, ‘Qty’, ‘Price’ ? You can choose to put any values of your choice.
Ans : Method – I
df[‘Val’] = 100
Method – II
df[‘Val’] = range(1,11)
or you can provide 10 different values.
12. Write code statements for a dataframe df for the following,
(a) Delete an existing column from it.
(b) Delete rows from 3 to 6 from it.
(c) Check if the dataframe has any missing values.
(d) Fill all missing values with 999 in it.
Ans:
(a) del df[‘ColumnName’]
or
df = df.drop(‘ColumnName’, axis = 1)
(b) df = df.drop(range(2,6) )
or
df = df.drop(range(2,6), axis = 0) #default axis is 0
(c) df.isna()
(d) df.fillna(999)
13. Write statement(s) to delete a row from a DataFrame.
Ans : df = df.drop(rowIndex)
14. Write statement(s) to delete a column from a DataFramee
Ans: del DF[‘ColumnNane’]
or
DF = DF.drop(‘ColumnName or Index’, axis = 1)
15. Write statement(s) to change the value at 5th row, 6th column in a DataFrame df.
Ans: df.iloc[4, 5 ] = Value
# 5th row means 4th index and 6th col means 5th index
OR
df.loc[5, 6 ] = value
# In case row index is 5 and column index is 6
OR df.at[5, 6] = value
# In case row index is 5 and column label is 6
16. Write statement(s) to change the values to 750 at 4th row to 9th row, 7th column in a DataFrame df.
Ans: df1.at[4:9,7] = 750
17. What is the difference between iloc and loc with respect to a DataFrame ?
Ans : loc and iloc both are used to access subset from a DataFrame. iloc measn Integer Location, while loc means location.
- iloc works with the index position while loc works with the row label and column label.
- iloc ignore the end index / position like slices while loc include both start label and end label.
18. What is the difference between iat and at with respect to a DataFrame ?
Ans: iat stands for integer at. iat and at both are used to select or access an individual data value from a dataframe.,
df.at[rowlabel, columnlable] e.g. df.at[‘Ram’, ‘Maths’]
df.iat[row index no, column index no] e.g. df.iat[1, 2]
- iat :- access a single value for a row and column pair by integer position.
- at :- access a single value for a row and column pair.
19. How would you delete columns from a dataframe ?
Ans: We can delete column(s) by using del statement or drop( ) method.
del dfObj[ColumnName]
Example:
del dfObj[5]
# 5 is a column label
del df2[‘Maths’]
# ‘Maths’ is a column lable
dfObj.drop( column index or sequence of column index, axis = 1) # axis = 1 for column
Example:
df.drop(4, axis=1)
df.drop([“Maths”, “Science”], axis = 1)
20. How would you delete rows from a dataframe ?
Ans: A rows can be deleted from a dataframe by using drop( ) method.
dfobj.drop(row index or indices)
Example :-
dfobj.drop(2)
dfobj.drop([3,4,6])
21. Which function would you use to rename the index/column names in a dataframe ?
Ans : rename ( ) function is use to change the name of any index or column names in a dataframe.
DF.rename(index = {<name dictionary>}, columns= {<name-dictionary}, inplace=True)
inplace = True => If you want to change in the same DataFrame.
inplace = False => if you want to change in the newly created dataframe.
<name-dictionary> means {oldindex : new index} or {old column name : new column name}
dfSale.rename(index={1:’One’, 2: ‘Two’}, columns = {‘math’: ‘Maths’, ‘sc’:’Science’. inplace=True}
Class 12 Informatics Practices [065] – Sumita Arora Book Exercise Solution
- Class 12 IP 065 Ch 1 Python Pandas I Type A Very Short Questions Answer
- Class 12 IP 065 Ch 1 Python Pandas 1 Type B Short Questions Answer
- Class 12 IP 065 Ch 1 Python Pandas 1 Type C Long Answer Questions
- Class 12 IP 065 Ch 2 Python Pandas II Type A Very Short Sumita Arora Book Questions Answer
- Class 12 IP 065 Ch 5 MySQL Revision Tour Check Point Questions Answer
- Class 12 IP 065 Ch 5 MySQL Revision Tour Assignment Questions Answer
- Class 12 IP 065 Chapter 9 Introduction to Computer Networks Sumita Arora Solution
- Class 12 IP 065 Chapter 9 Introduction to Computer Networks Assignments Sumita Arora Solution
- Class 12 Informatics Practices Chapter 6 MySQL Functions Check Point Sumita Arora Exercise Solution
- Class 12 Informatics Practices Chapter 6 MySQL Functions Assignments Sumita Arora Exercise Solution
- Class 12 Informatics Practices 065 Ch 3 Plotting with PyPlot Sumita Arora Book Exercise Solution
- Class 12 Informatics Practices 065 Ch 4 Importing Exporting Data Between CSV Files MySQL and Pandas Sumita Arora Book Exercise Solution
Sumita Arora Solution
- Class 12 Informatics Practices (065) Sumita Arora Book Exercise Solution
- CBSE Class 12 Computer Science Sumita Arora Book Exercise Solution
- Class 11 Informatics Practices (065) Sumita Arora Book Exercise Solution