Skip to content

Add names to dataframe - #235

Merged
jm-rivera merged 6 commits into
datacommonsorg:masterfrom
ONEcampaign:add-names-to-dataframe
Apr 8, 2025
Merged

Add names to dataframe#235
jm-rivera merged 6 commits into
datacommonsorg:masterfrom
ONEcampaign:add-names-to-dataframe

Conversation

@jm-rivera

@jm-rivera jm-rivera commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

This should hopefully be the last 2.0.0 feature - exciting times!

TL:DR: Improve the Observations DataFrame to include entity and variable names.


This PR:

  • Adds add_entity_names_to_observations_dataframe which inserts resolved names for specific columns in a dataframe
  • Adds flatten_names_dictionary which converts a dictionary of Name objects into a flat dcid->name dict.
  • Modifies .observations_dataframe in order to insert names for entity and variable for a more user-friendly DataFrame

For

from datacommons_client import DataCommonsClient

dc = DataCommonsClient(dc_instance="datacommons.one.org")
data = dc.observations_dataframe(
    variable_dcids=["Count_Person"],
    date="latest",
    entity_dcids=["country/TGO", "country/SSD", "country/ZWE"],
)

Previously

date entity variable value facetId importName measurementMethod observationPeriod provenanceUrl unit
0 2023 country/ZWE Count_Person 16340822 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
1 2016 country/ZWE Count_Person 16150000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None
2 2023 country/TGO Count_Person 9304337 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
3 2016 country/TGO Count_Person 7606000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None
4 2023 country/SSD Count_Person 11483374 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
5 2016 country/SSD Count_Person 12231000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None

With this PR

date entity entity_name variable variable_name value facetId importName measurementMethod observationPeriod provenanceUrl unit
0 2023 country/ZWE Zimbabwe Count_Person Total Population 16340822 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
1 2016 country/ZWE Zimbabwe Count_Person Total Population 16150000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None
2 2023 country/TGO Togo Count_Person Total Population 9304337 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
3 2016 country/TGO Togo Count_Person Total Population 7606000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None
4 2023 country/SSD South Sudan Count_Person Total Population 11483374 3981252704 WorldDevelopmentIndicators None P1Y https://datacatalog.worldbank.org/dataset/world-development-indicators/ None
5 2016 country/SSD South Sudan Count_Person Total Population 12231000 473499523 Subnational_Demographics_Stats WorldBankSubnationalPopulationEstimate P1Y https://databank.worldbank.org/source/subnational-population None

@kmoscoe FYI for the docs.

To `to_observation_records`
- Add `add_entity_names_to_observations_dataframe` which inserts resolved names for specific columns in a dataframe
- Add `flatten_names_dictionary` which converts a dictionary of `Name` objects into a flat dcid->name dict.
- Modifies `.to_observation_records` in order to insert names for `entity` and `variable` for a more userfriendly DataFrame
@jm-rivera
jm-rivera requested review from dwnoble and keyurva April 8, 2025 16:03
@jm-rivera jm-rivera self-assigned this Apr 8, 2025
@jm-rivera jm-rivera mentioned this pull request Apr 8, 2025
@jm-rivera
jm-rivera merged commit d6b21c2 into datacommonsorg:master Apr 8, 2025
@jm-rivera
jm-rivera deleted the add-names-to-dataframe branch April 8, 2025 20:04
@kmoscoe

kmoscoe commented Apr 8, 2025 via email

Copy link
Copy Markdown
Contributor

@kmoscoe

kmoscoe commented Apr 8, 2025 via email

Copy link
Copy Markdown
Contributor

@kmoscoe

kmoscoe commented Apr 9, 2025 via email

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants