Search code examples
pythongeopandas

Attaching parameters to geojson object becomes non-existant when creating a geopandas dataframe


I have this dataframe

d = {
    'geoid': ['13085970205'],
    'FIPS': ['13085'],
    'Year': [2024],
    'parameters': [{"Year": 2024, "hpi_prediction": 304.32205}],
    'geometry':[
        {
            "coordinates": [[[[-84.126456, 34.389734], [-84.12641, 34.39026], [-84.126323, 34.39068]]]],
            "parameters": {"Year": 2024, "hpi_prediction": 304.32205},
            "type": "MultiPolygon"
        }
    ]
    
}

dd = pd.DataFrame(data=d)

When I want to write this out I use import geopandas as gpd to convert the data into a dataframe like this

df_geopandas_hpi = gpd.GeoDataFrame(dd[['geoid', 'geometry']])

Once this happens the parameters key in the original dataframe gets erased. Why? Note that the type of geometry in example dataframe is geojson.geometry.MultiPolygon. How can I avoid this from happening?

What I essentially need to do is the following

if ~os.path.exists('../verus_data'):
    os.mkdir('../verus_data')

for county, df_county in dd.groupby('FIPS'):
    if ~os.path.exists('../verus_data/'+str(county)):
        os.mkdir('../verus_data/'+str(county))

    if ~os.path.exists('../verus_data/'+str(county)+'/'+'predicted'):
        os.mkdir('../verus_data/'+str(county)+'/'+'predicted')

    if ~os.path.exists('../verus_data/'+str(county)+'/'+'analyzed'):
        os.mkdir('../verus_data/'+str(county)+'/'+'analyzed')    

    df_hpi = df_county[df_county['key'] == 'hpi']
    df_analyzed = df_county[df_county['key'] == 'analyzed']

    for year, df_year in df_hpi.groupby('Year'):
        if ~os.path.exists('../verus_data/'+str(county)+'/'+'predicted'+'/'+str(year)):
            os.mkdir('../verus_data/'+str(county)+'/'+'predicted'+'/'+str(year))

            df_geopandas_hpi = gpd.GeoDataFrame(df_year[['geoid', 'geometry', 'parameters']])
            df_geopandas_hpi.to_file('../verus_data/'+str(county)+'/'+'predicted'+'/'+str(year)+'/'+'hpi_predictions.geojson', driver="GeoJSON")

    for year, df_year in df_analyzed.groupby('Year'):
        if ~os.path.exists('../verus_data/'+str(county)+'/'+'analyzed'+'/'+str(year)):
            os.mkdir('../verus_data/'+str(county)+'/'+'analyzed'+'/'+str(year))

            df_geopandas_analyzed = gpd.GeoDataFrame(df_year[['geoid', 'geometry', 'parameters']])
            df_geopandas_analyzed.to_file('../verus_data/'+str(county)+'/'+'analyzed'+'/'+str(year)+'/'+'analyzed_values.geojson', driver="GeoJSON")

I need to somehow write out these geojson files while keeping parameters key intact.


Solution

  • All you have to do is add the parameters column in the

    df_geopandas_hpi = gpd.GeoDataFrame(df_year[['geoid', 'geometry', 'parameters']])