Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import numpy as np
  2.  
  3. STATE_CODE = 'state_code'
  4. COUNTY_CODE = 'county_code'
  5. TRACT_CODE = 'tract_code'
  6.  
  7. def convert_block_code_to_str(df):
  8. """
  9. A function to convert the block_code Series to a 15-character string.
  10.  
  11. Params
  12. df(pandas.DataFrame): A Pandas DataFrame with a block_code column.
  13. """
  14. return df.assign(block_code=df.block_code.astype(str).str.zfill(15))
  15.  
  16.  
  17. def add_geographies(df, geographies):
  18. """
  19. A function to add subcategories of a full 15-character block code, such as
  20. a state_code, county_code, or tract_code. If the block_code column of df
  21. is not a string, it will first be converted to a string.
  22.  
  23. Params
  24. df(pandas.DataFrame): A Pandas DataFrame with a block_code column.
  25. geographies(list): A list of geographies
  26.  
  27. Return:
  28. The given pandas.DataFrame with the given geographies added, assuming the
  29. geographies are valid.
  30. """
  31. if not (df.block_code.dtype == np.dtype('O')):
  32. df = convert_block_code_to_str(df)
  33.  
  34. for geography in geographies:
  35. if geography == STATE_CODE:
  36. df = df.assign(state_code=df.block_code.str[0:2])
  37. elif geography == COUNTY_CODE:
  38. df = df.assign(county_code=df.block_code.str[0:5])
  39. elif geography == TRACT_CODE:
  40. df = df.assign(tract_code=df.block_code.str[0:11])
  41. else:
  42. print("Geography {} not recognized".format(geography))
  43. return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement