Informatics Practices
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.
Python Pandas
4 Likes
Answer
The syntax to add a new column to a DataFrame is <DF object>.[<column>] = <new value>. Therefore, according to this syntax, the statement to add a column named 'val' to a dataframe df with 10 rows is :
df['val'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Answered By
3 Likes
Related Questions
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.
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.
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.
Write statement(s) to delete a row from a DataFrame.