Guest User

Untitled

a guest
Apr 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. def benford_function(value):
  5. if value == '':
  6. return []
  7.  
  8. if ("." in value):
  9. before_decimal=value.split(".")[0]
  10. if len(before_decimal)==0:
  11. bd_first="0"
  12. bd_second="0"
  13.  
  14. if len(before_decimal)>1:
  15. before_decimal=before_decimal[:2]
  16. bd_first=before_decimal[0]
  17. bd_second=before_decimal[1]
  18. elif len(before_decimal)==1:
  19. bd_first="0"
  20. bd_second=before_decimal[0]
  21.  
  22. after_decimal=value.split(".")[1]
  23. if len(after_decimal)>1:
  24. ad_first=after_decimal[0]
  25. ad_second=after_decimal[1]
  26. elif len(after_decimal)==1:
  27. ad_first=after_decimal[0]
  28. ad_second="0"
  29. else:
  30. ad_first="0"
  31. ad_second="0"
  32.  
  33.  
  34.  
  35. else:
  36. ad_first="0"
  37. ad_second="0"
  38. if len(value)>1:
  39. bd_first=value[0]
  40. bd_second=value[1]
  41. else:
  42. bd_first="0"
  43. bd_second=value[0]
  44. return pd.Series([bd_first,bd_second,ad_first,ad_second])
  45.  
  46. df.apply(lambda row: benford_function(row['a']), axis=1)
  47.  
  48. 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