Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- def benford_function(value):
- if value == '':
- return []
- if ("." in value):
- before_decimal=value.split(".")[0]
- if len(before_decimal)==0:
- bd_first="0"
- bd_second="0"
- if len(before_decimal)>1:
- before_decimal=before_decimal[:2]
- bd_first=before_decimal[0]
- bd_second=before_decimal[1]
- elif len(before_decimal)==1:
- bd_first="0"
- bd_second=before_decimal[0]
- after_decimal=value.split(".")[1]
- if len(after_decimal)>1:
- ad_first=after_decimal[0]
- ad_second=after_decimal[1]
- elif len(after_decimal)==1:
- ad_first=after_decimal[0]
- ad_second="0"
- else:
- ad_first="0"
- ad_second="0"
- else:
- ad_first="0"
- ad_second="0"
- if len(value)>1:
- bd_first=value[0]
- bd_second=value[1]
- else:
- bd_first="0"
- bd_second=value[0]
- return pd.Series([bd_first,bd_second,ad_first,ad_second])
- df.apply(lambda row: benford_function(row['a']), axis=1)
- df[['bd_first'],['bd_second'],['ad_first'],['ad_second']]= df.apply(lambda row: benford_function(row['a']), axis=1)
Add Comment
Please, Sign In to add comment