Q. Consider the following code to create two dataframes with similar values. What will be printed by the code given below? Justify your answer.


df1 = pd.DataFrame ([1, 2, 3])

df2 = pd.DataFrame([[1, 2, 3]])

print("df1")

print (df1)

print("df2")

print (df2)


Answer :-

The output produced by above code will be as shown here.

df1
   0
0  1
1  2
2  3
df2
   0  1  2
0  1  2  3

The reason being that the first dataframe dfl is created with a 1D list hence all its values are taken as new row for dataframe df1 and only one column is created. df1 is like the one shown on left below. Dataframe df2, is created with a 2D list where inner list contains values 1,2, 3. Since inner list forms the column values, the three columns are formed from the three values of inner list; there is just one inner list, hence one row only.


Dataframe df2 is like the one shown on the right below:

df1



0
0 1
1 2
2 2

df2

0 1 2
0 1 2 3

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post