layout | title | nav_order | parent | grand_parent |
---|---|---|---|---|
default |
Get a single statistical value for a place |
3 |
Python |
API - Query data programmatically |
Returns a statistical value for a place based on the
StatisticalVariable
.
See the full list of StatisticalVariable
classes.
When there are multiple sources for the same statistical variable, a preferred source with more recent data or more authority is selected.
Signature:
datacommons.get_stat_value(place, stat_var, date=None, measurement_method=None,observation_period=None, unit=None, scaling_factor=None)
Required arguments:
place
: The DCID of thePlace
to query for.stat_var
: The DCID of theStatisticalVariable
.
You can find a list of StatisticalVariables with human-readable names here.
Optional arguments:
date
: The preferred date of observation in ISO 8601 format. If not specified, returns the latest observation.measurement_method
: The DCID of the preferredmeasurementMethod
value.observation_period
: The preferredobservationPeriod
value.unit
: The DCID of the preferredunit
value.scaling_factor
: The preferredscalingFactor
value.
Going into more detail on how to assemble the values for the required arguments:
place
: For this parameter, you will need to specify the DCID (the unique ID assigned by Data Commons to each node in the graph) of the place you are interested in.stat_var
: The statistical variable whose value you are interested in.
In addition to these required properties, this method also allows for other, optional arguments. Here are helpful arguments in regular use by Data Commons developers:
-
date
: Specified in ISO 8601 format. Examples include2011
(the year 2011),2019-06
(the month of June in the year 2019), and2019-06-05T17:21:00-06:00
(5:17PM on June 5, 2019, in CST). -
measurement_method
: The technique used for measuring a statistical variable. -
observation_period
: The time period over which an observation is made. -
unit
: The unit of measurement. -
scaling_factor
: Property of statistical variables indicating factor by which a measurement is multiplied to fit a certain format.
The method will return a simple number, like '1.20949' or '1431252'.
>>> datacommons.get_stat_value("geoId/05", "Count_Person_Male")
1474705
>>> datacommons.get_stat_value("geoId/05", "Count_Person_Male", date="2012")
1431252
Example 3: Retrieve the number of people in Bosnia and Herzegovina as counted by the Bosnian census.
>>> datacommons.get_stat_value("country/BIH", "Count_Person", measurement_method="BosniaCensus")
3791622
>>> datacommons.get_stat_value("geoId/12086", "Count_Death", observation_period="P1Y")
20703
>>> datacommons.get_stat_value("geoId/12086", "RetailDrugDistribution_DrugDistribution_Naloxone", unit="Grams")
118.79
Example 6: Retrieve the percentage of nominal GDP spent by the government of the Gambia on education.
>>> datacommons.get_stat_value("country/GMB", "Amount_EconomicActivity_ExpenditureActivity_EducationExpenditure_Government_AsFractionOf_Amount_EconomicActivity_GrossDomesticProduction_Nominal", scaling_factor="100.0000000000")
2.43275
If there is no value associated with the requested property, nan
is returned:
>>> datacommons.get_stat_value("geoId/1001", "Count_Person_Male")
nan
If you do not pass a required positional argument, a TypeError is returned:
>>> datacommons.get_stat_value("geoId/1001")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get_stat_value() missing 1 required positional argument: 'stat_var'