[GIS] Selecting features from shapefile where field=”value” using Geopandas

gdalgeopandasogrpython

I don't understand the difference between OGR and Geopandas librairies.
I have checked the documentation of GDAL as well as http://geopandas.org/ but I still don't get it.
I just started learning about them and for now I'm trying to write some scripts in python using Geopandas.

My goal is to select features from a shapefile where field="value", then export the selected features to a shapefile.
The second step would be to add another field to the output shapefile(with only the selected features from the first shapefile) and set it to a certain value.
Doing the second part with OGR seems easy. This is how I've done it :

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSrc = driver.Open("myshp.shp", 1)
fld = ogr.FieldDefn('City', ogr.OFTString)
lyr = dataSrc.GetLayer()
lyr.CreateField(fld)
for feature in lyr:
    feature.SetField("Not Found", fld)
    layer.SetFeature(feature)
dataSrc= None

However I don't see how I can do the same thing using Geopandas. I might be missing the point though that's why I'm confused between the use of the two librairies.

I know that this is easily done using QGIS or ArcGIS manually, but as of now I'm trying to learn how to use python with Geopandas.

How can I achieve my goal explained above using Geopandas?

Best Answer

I didn't knew geopandas, it is very easy to add a field, a great library indeed. You just need to read your file add it and save.

dataSrc = gpd.read_file('my_shp.shp')
dataSrc['new_field'] = 1
dataSrc.to_file('newfile.shp')

Selecting is also straightforward:

dataSrc = gpd.read_file('my_shp.shp')
dataSrc[dataSrc['id']<4].to_file('new_shape.shp')