Q. Given two dataframe df3 and df4 as shown below:


df3

    A    B    C
0    100    200    300
1    400    500    600

df4

    A    B
0    1000    2000
1    4000    5000
2    7000    8000

df3 + df4

    A    B    C
0    1100.0    2200.0    NaN
1    4400.0    5500.0    NaN
2    NaN    NaN    NaN

Both these data-frames store integer values but when they are added as df3+ df4, the values in the resultant object automatically change to floating point (as shown on above right) contrary to the fact the two integers when added will result into integer only. Can you specify the reason ?


Answer :-

The reason behind the conversion to floating point type is that the two dataframes have different indexes and columns. For the non-matching row indexes and columns, Python will add NaN values to corresponding value from another dataframe.

Python stores NaN values in a non-integer suitable data type. Thus, the moment NaN is added or present in any column, the datatype of the entire column is changed. Thus, all the values are represented as floating point value because of the presence of NaN values in their column.

Post a Comment

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

Previous Post Next Post