8

How to extract the decimal number part from a float (float64) in a dataframe? (a very common scenario but I can 't find a solution in StackOverFlow)

Note: be careful with the 196.09, I need 09, not 9.

Sample DataFrame:

    dollars Count
0   56.46   2
1   196.09  3
2   78.12   2

Expected result is the 2 decimal digits:

    decimal
0   46
1   09
2   12
smci
  • 32,567
  • 20
  • 113
  • 146
Learn
  • 528
  • 2
  • 6
  • 18
  • Just because you want leading-zero padding on the result, you don't want `09` returned for `196.09`. You could either have a string result (not an int, slow, can't do further arithmetic on it), or (better) you can use an int result, just remember to use `f'{number:02d}'` or `'{:02d}'.format` when you display it. Or simply set `pd.options.display.float_format = '{:02d}'.format`, or see [How to display pandas DataFrame of floats using a format string for columns?](https://stackoverflow.com/questions/20937538/how-to-display-pandas-dataframe-of-floats-using-a-format-string-for-columns) – smci Feb 13 '22 at 00:49

3 Answers3

7

Use numpy.modf, multiple by 100 and cast to integers:

df['decimal'] = (np.modf(df['dollars'])[0] * 100).astype(int)

Or split by .:

df['decimal'] = df['dollars'].astype(str).str.split('.').str[1].astype(int)

print (df)
   dollars  Count  decimal
0    56.46      2       46
1   196.69      3       68
2    78.12      2       12

EDIT: If need 09 format need second solution - output is strings:

df['decimal'] = df['dollars'].astype(str).str.split('.').str[1]
print (df)
   dollars  Count decimal
0    56.46      2      46
1   196.09      3      09
2    78.12      2      12
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
5

Alternative 1

An alternative would be to convert dollars to string, then use a regex to extract everything after the .:

df['decimal'] = df.dollars.astype(str).str.extract('\.(.*)').astype(int)

>>> df
   dollars  Count  decimal
0    56.46      2       46
1   196.69      3       69
2    78.12      2       12

Alternative 2

Or, you could subtract dollars from the int part of dollars, and multiply by 100:

df['decimal'] = (df.dollars.sub(df.dollars.astype(int))).mul(100).astype(int)

>>> df
   dollars  Count  decimal
0    56.46      2       46
1   196.69      3       68
2    78.12      2       12

Edit: based on the edit to OP's question, it seems that the decimal parts need to be displayed to 2 decimal points (e.g. it would need to be 09 instead of 9). In this case, It has to be displayed as a string, and not an int. The first method I outlined above would still work if you omit astype(int):

df['decimal'] = df.dollars.astype(str).str.extract('\.(.*)')

>>> df
   dollars  Count decimal
0    56.46      2      46
1   196.09      3      09
2    78.12      2      12

Or, this could be done after the fact using zfill, if we already have the decimal part as an int:

df['decimal'] = df['decimal'].astype(str).str.zfill(2)
sacuL
  • 49,704
  • 8
  • 81
  • 106
3

If you know you have 2 decimal places.. just use % broadcasting

s = df.dollars % 1 * 100

0    46.0
1    69.0
2    12.0
Name: dollars, dtype: float64

s.astype(int)

0    46
1    69
2    12
rafaelc
  • 57,686
  • 15
  • 58
  • 82