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
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)