KnowledgeBoat Logo
|
LoginJOIN NOW

Informatics Practices

Given a dataframe df as shown below :

 ABD
0151719
1161820
2202122

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]

Python Pandas

6 Likes

Answer

(a) df['C'] = np.NaN — This statement will add a new column 'C' to the dataframe and assign np.NaN (Not a Number) to all rows in this new column.

The updated dataframe will look like this:

Output
    A   B     D    C
0   15  17   19  NaN
1   16  18   20  NaN
2   20  21   22  NaN

(b) df['C'] = [2, 5] — This statement will result in error because the length of the list [2, 5] does not match the number of rows in the DataFrame df.

(c) df['C'] = [12, 15, 27] — This statement will add a new column 'C' to the dataframe and assign the values from the list [12, 15, 27] to the new column. This time, all rows in the new column will be assigned a value.

The updated dataframe will look like this:

Output
    A   B   D   C
0  15  17  19  12
1  16  18  20  15
2  20  21  22  27

Answered By

2 Likes


Related Questions