Informatics Practices
Hitesh wants to display the last four rows of the dataframe df and has written the following code :
df.tail()
But last 5 rows are being displayed. Identify the error and rewrite the correct code so that last 4 rows get displayed.
Python Pandas
6 Likes
Answer
The error in Hitesh's code is that the tail()
function in pandas by default returns the last 5 rows of the dataframe. To display the last 4 rows, Hitesh needs to specify the number of rows he wants to display.
Here's the correct code:
df.tail(4)
Answered By
2 Likes
Related Questions
Given a dataframe df as shown below :
A B D 0 15 17 19 1 16 18 20 2 20 21 22 What will be the result of following code statements ?
(a) df['C'] = np.NaN
(b) df['C'] = [2, 5]
(c) df['C'] = [12, 15, 27]
Write code statements to list the following, from a dataframe namely sales:
(a) List only columns 'Item' and 'Revenue'.
(b) List rows from 3 to 7.
(c) List the value of cell in 5th row, 'Item' column.
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.
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.