Search code examples
pythonpandasdataframegeopandaspoint-in-polygon

Pandas update column value if df1 index in df2 index


I have a dataframe of coordinates in which I perform point-in-polygon and it returns the set of points in the polygon.

df1 - Original coordinates

0              POINT (-97.96192929999999 29.8929939)
1              POINT (-97.98886109999999 29.8230438)
2              POINT (-97.6573715 30.15241810000001)
3              POINT (-97.68809509999998 30.3590794)
4              POINT (-97.37609860000001 31.0930271)
5              POINT (-97.66625980000001 30.3466492)
6                     POINT (-97.6666412 30.3455753)
...

df2 - Results

4       POINT (-97.37609860000001 31.0930271)
1496    POINT (-97.64907839999999 30.3872128)
445     POINT (-97.64907839999999 30.3872128)
2822    POINT (-97.649353 30.387228)
1369    POINT (-97.6488342 30.3873215)
6       POINT (-97.6666412 30.3455753)
2303    POINT (-97.6492767 30.38755039999999)
...

How would I add an "area" column in df1 and set the values = "area1" for the row indexes that are in both dfs? In the example above, rows 4 and 6 are in the results so I'd like to have an area column = "area1" for those rows in df1


Solution

  • If you are talking about the index, cause the pandas is index/column sensitive, which mean it will assign the new value base on the index matched.

    So what you can do

    df1['area']=df2['Value']
    #df2['New']='are1' then df1['area']=df2['New']
    

    Toy data

    df1=pd.DataFrame({'d1':[1,2,3]},index=[1,2,3])
    df2=pd.DataFrame({'d2':[1,2,3]},index=[2,6,1])
    df1['New']=df2.d2
    df1
    Out[724]: 
       d1  New
    1   1  3.0
    2   2  1.0
    3   3  NaN