0

I'm trying to set the value of a column in a pandas data frame to some big numbers with a simple line:

df['Capital'] = 58143898.13876611

and it shows in df as format 5.814380e+07. I want it as 58143898.

What I've tried:

df['Capital'] = int(58143898.13876611)

Similar question: How to print all digits of a large number in python?, but it's outdated because I learned from NameError: global name 'long' is not defined that long is replaced by int in python 3. But it still shows 5.814380e+07.

Yet if I only print the following line, it does show as 58143898.

In [2] int(58143898.13876611)

Out[2] 58143898

Please help! Thank you so much in advance :)

LSF
  • 97
  • 6

1 Answers1

2

You are complaining that pandas represents your number in floating point rather than integer form. Coldspeed points out that .astype() can change a column to some desired type.

To better understand what pandas is doing with your input, look at:

df.dtypes

You want the Capital column to have an integer type.

To avoid the need to monkey about with types later, you may want to take care to convert inputs to int before constructing the dataframe. Pandas looks at all values in a column and chooses a type compatible with the entire set of values. So e.g. a column containing [1, 2.5, 3] will force all values to float rather than int.

It's worth noting that missing values can have very noticeable effects on this. You may want something like:

df2 = df1.dropna()

Certain FP bit patterns are reserved for use as NaN, but pandas regrettably does not reserve maxint, nor any other integer value, to model the notion of missing data. Accordingly an input like [1, None, 3] will be promoted from int to float.

J_H
  • 17,926
  • 4
  • 24
  • 44