Skip to content
Advertisement

Can’t multiply 2 columns in pandas

I have an issue. As you see the following example:

data = pd.DataFrame({'a':[127093342616], 'b':[22853943721]})
data['a*b']= data['a']*data['b']
data
  • When I multiply 2 numbers: 127093342616 vs 22853943721, the result = 2904584099459834914136
  • But, when I use DataFrame of pandas and multiply the 2 columns the result = 8445279887435310424

Can anybody tell me the reason this occurs and a solution?

Advertisement

Answer

Try using float datatype, you’re having integer overflow problems:

data = pd.DataFrame({'a':[127093342616], 'b':[22853943721]}, dtype=np.float)
data['a*b']= data['a']*data['b']
data

Output:

              a             b           a*b
0  1.270933e+11  2.285394e+10  2.904584e+21

Because pandas use numpy underneath, let’s look at numpy datatypes.

numpy.int64 – Integer (-9223372036854775808 to 9223372036854775807)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement