Search code examples
pandasdataframemergegeometrygeopandas

merging two geodataframes with no common elements but one is a single columned geometry column


Good afternoon,

I am using geopandas in python3 and I have a small issue that I have been stuck with and cannot seem to resolve. I have two geodataframes with the same number of rows. The first geodataframe has 1700 rows with 6 columns and the second geodataframe has 1700 rows with a single column but that single column is a geometry column.

I am simply trying to add that single column geometry dataframe to the last column of the first geodataframe that has regular statistics making sure that it remains a geometry column.

So my first geodataframe, df1 looks like this...

    Mean    Std Dev  Max    Min   Day    Date
0   174.7   417.9   6904    0   2019003 2019-01-03
1   190.4   402.8   6876    0   2019004 2019-01-04

and the second geodataframe, df2 looks something like this...

                     geometry
0   POLYGON ((-78 43, -78 44))
1   POLYGON ((-78 43, -78 44))

What it should look like when joined properly is something like this as a single geodataframe, df3

        Mean      Std Dev    Max    Min   Day    Date         geometry
0   174.765941  417.942834  6904    0   2019003 2019-01-03    POLYGON((-78 43, -78 44))
1   190.446559  402.876998  6876    0   2019004 2019-01-04    POLYGON((-78 43, -78 44))

I have tried appending, that didnt work, I have tried inserting, that didnt work and I have tried concatenating and that didnt work.

What am I missing and how would I do this please.

Thanks in advance for all the help.


Solution

  • Use pd.concat:

    import pandas as pd
    import geopandas as gpd
    
    df3 = gpd.GeoDataFrame(pd.concat([df1, df2], axis=1), crs=df2.crs)
    

    Output:

        Mean  Std Dev   Max  Min      Day        Date                   geometry
    0  174.7    417.9  6904    0  2019003  2019-01-03  POLYGON((-78 43, -78 44))
    1  190.4    402.8  6876    0  2019004  2019-01-04  POLYGON((-78 43, -78 44))