No Description

fetchTables.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import pandas as pd
  2. import rpy2.robjects as robjects
  3. #hellooo
  4. robjects.r('''
  5. library(tidycensus)
  6. library(tidyverse)
  7. tableYear = 2020
  8. #log on with API
  9. census_api_key("7a853acf81fd5758228680556ac831138c40b83e")
  10. #load variables
  11. pueblos = 78
  12. variables = load_variables(2020,"acs5/profile")
  13. codes = variables$name
  14. label0 = variables$label
  15. #view variables
  16. #varTable = table(variables$concept)
  17. #write.table(varTable, file = "cat.txt", sep = ",", quote = FALSE, row.names = F)
  18. #pull all tables with every variable
  19. test = get_acs(geography = "county",
  20. state = "PR",
  21. year = tableYear,
  22. variables = codes)
  23. #add label column
  24. #create empty vector
  25. labelCol = c()
  26. #amount of GEOIDS
  27. for (x in 1:pueblos){
  28. labelCol = c(labelCol, label0)
  29. }
  30. #combine test and cols
  31. test["label"] = labelCol
  32. #rearrange cols
  33. test = test[c("NAME","label","variable","estimate","moe","GEOID")]
  34. GEOIDS = table(test$GEOID)
  35. #omit NA rows
  36. noNA = na.omit(test)
  37. DP02table = noNA %>% filter(startsWith(variable,"DP02"))
  38. DP03table = noNA %>% filter(startsWith(variable,"DP03"))
  39. DP04table = noNA %>% filter(startsWith(variable,"DP04"))
  40. DP05table = noNA %>% filter(startsWith(variable,"DP05"))
  41. ''')
  42. table02 = robjects.r('''DP02table''')
  43. table03 = robjects.r('''DP03table''')
  44. table04 = robjects.r('''DP04table''')
  45. table05 = robjects.r('''DP05table''')
  46. print(table02)