Ad

Getting Length Of Fractional Part Of A Decimal

- 1 answer

I want to get length of fractional part ex : 0.063 - 3 , 0.04 -2 i got fractional part using this

df['fractional_part'] = np.modf(df['mod_text'])[0]

I tried to get count of fractional part using this

df['count_fractional_part']  = df['fractional_part'].apply(lambda x:int(len(str(x).split('.')[1])))

But i want to do it with out apply function Is there any way doing it with numpy

Ad

Answer

Convert values to strings, then use Series.str.split, select second value and get length by Series.str.len:

df = pd.DataFrame({'mod_text':[0.063, 0.04]})

df['count_fractional_part'] = df['mod_text'].astype(str).str.split('.').str[1].str.len()

print (df)
   mod_text  count_fractional_part
0     0.063                      3
1     0.040                      2
Ad
source: stackoverflow.com
Ad