I have two dataframes , First dataframe contains data from given location and the second dataframe has data with coordinates to identify those locations from first dataframe.
I want to compare column values of locations from first dataframe to county values from the second dataframe then attach the coordinates at the exact location name on the first dataframe. Here is the first dataframe:
import pandas as pd
boxes = {'ID': ['43001','43002','43003','43004','43005','43006','43007','43008'],
'Location': ['Busia','Nairobi','Tharaka','Kakamega','Kajiado','Meru','NaN','Kiambu']
}
df = pd.DataFrame(boxes, columns = ['ID', 'Location'])
print(df)
Second dataframe
import pandas as pd
coord = {'ID': ['456001','653002','783003','4533004','900005','4367006','643007','943008'],
'county': ['Busia','Nairobi','Tharaka','Kakamega','Kajiado','Meru','NaN','Kiambu'],
'geometry': ['POLYGON ((35.79593))','POLYGON ((355.79593))','POLYGON ((35.79593))','POLYGON ((535.79593))','POLYGON ((835.79593))','POLYGON ((735.79593))','POLYGON ((335.79593))','POLYGON ((635.79593))']
}
df1 = pd.DataFrame(coord, columns = ['ID', 'county', 'geometry'])
print(df1)
Intention is compare Location values from first dataframe to County values from second dataframe. If the names are the same then attach geometry values for those specific locations on first dataframe.
End result to look like this
import pandas as pd
boxes = {'ID': ['43001','43002','43003','43004','43005','43006','43007','43008'],
'Location': ['Busia','Nairobi','Tharaka','Kakamega','Kajiado','Meru','NaN','Kiambu'],
'geometry': ['POLYGON ((35.79593)','POLYGON ((335.79593))','POLYGON ((35.79593))','POLYGON ((635.79593))', 'POLYGON ((835.79593))','POLYGON ((735.79593))','NaN','POLYGON ((535.79593))']
}
df = pd.DataFrame(boxes, columns = ['ID', 'Location', 'geometry'])
print(df)
I tried this but didnt work well as expected
if df['Location'].values == df1['county'].values:
df2 = pd.concat([df, df1['geometry']], axis=1)
df2