pytidycensus.variables
Census variable loading and caching functionality.
Functions
|
Clear the variables cache. |
|
Get all variables for a specific table. |
|
List available datasets for a given year. |
|
Load Census variables for a given dataset and year. |
|
Search for variables by pattern in labels, concepts, or names. |
- pytidycensus.variables.load_variables(year, dataset, survey=None, cache=True, cache_dir=None)[source]
Load Census variables for a given dataset and year.
- Parameters:
year (int) – Census year
dataset (str) – Dataset name (‘acs’, ‘dec’, ‘pep’, etc.)
survey (str, optional) – Survey type (e.g., ‘acs5’, ‘acs1’, ‘sf1’, ‘pl’)
cache (bool, default True) – Whether to cache variables for faster future access
cache_dir (str, optional) – Directory for caching. Defaults to user cache directory.
- Returns:
Variables with columns: name, label, concept, predicateType, group, limit
- Return type:
pd.DataFrame
Examples
>>> # Load ACS 5-year variables for 2022 >>> acs_vars = load_variables(2022, "acs", "acs5") >>> >>> # Search for income-related variables >>> income_vars = acs_vars[acs_vars['label'].str.contains('income', case=False)] >>> >>> # Load decennial census variables for 2020 >>> dec_vars = load_variables(2020, "dec", "pl")
- pytidycensus.variables.search_variables(pattern, year, dataset, survey=None, field='label')[source]
Search for variables by pattern in labels, concepts, or names.
- Parameters:
- Returns:
Matching variables
- Return type:
pd.DataFrame
Examples
>>> # Search for income variables in ACS >>> income_vars = search_variables("income", 2022, "acs", "acs5") >>> >>> # Search for population in concepts >>> pop_vars = search_variables("population", 2020, "dec", "pl", field="concept")
- pytidycensus.variables.get_table_variables(table, year, dataset, survey=None)[source]
Get all variables for a specific table.
- Parameters:
- Returns:
Variables for the specified table
- Return type:
pd.DataFrame
Examples
>>> # Get all variables for median household income table >>> b19013_vars = get_table_variables("B19013", 2022, "acs", "acs5") >>> >>> # Get all variables for race table in 2020 Census >>> p1_vars = get_table_variables("P1", 2020, "dec", "pl")