new to coding and first time posting so let me know if there is something I haven't included. I am trying to add new columns to a shapefile. I have a list of columns that are required and sometimes the shapefile will have only some of these. So I need to check which ones are present and add those which are not. I have tried to achieve this using the following code:
import geopandas as gpd
shape = gpd.read_file("shapefile.shp")
add_fields = [['GUI',
'POLYGON',
'ORIG_HAB',
'ORIG_CLASS',
'EUNIS_L3',
'HAB_TYPE',
'VERSION',
'DET_MTHD',
'DET_NAME',
'TRAN_COM',
'T_RELATE',
'VAL_COMM',
'DataAccess',
'Three_Step',
'MESH_Confi']]
field_name_list = shape.columns.tolist()
for fieldToAdd in add_fields:
if fieldToAdd not in field_name_list:
shape.reindex(shape.columns.tolist() fieldToAdd, axis=1)
shape.to_file("outputshapefile.shp")
When I run the code there are no errors, however the shapefile appears unaltered. I have scoured the forums to see what to try next and that's partly where I found the .reindex. I think that I am not actually adding the new columns but not sure how to do this. If there is anyone can that can point me in the right direction it would be very much appreciated.
CodePudding user response:
I think you are looking for something like this:
import geopandas as gpd
shapefile = gpd.read_file("shapefile.shp")
add_fields = ['GUI',
'POLYGON',
'ORIG_HAB',
'ORIG_CLASS',
'EUNIS_L3',
'HAB_TYPE',
'VERSION',
'DET_MTHD',
'DET_NAME',
'TRAN_COM',
'T_RELATE',
'VAL_COMM',
'DataAccess',
'Three_Step',
'MESH_Confi']
field_name_list = shapefile.columns.tolist()
for fieldToAdd in add_fields:
if fieldToAdd not in field_name_list:
#shapefile.reindex(shapefile.columns.tolist().append(fieldToAdd), axis=1)
shapefile[fieldToAdd]=None
shapefile.to_file("outputshapefile.shp")
Please notice that I've changed add_fields to be a 1D instead of 2D (removed outer brackets) Also I changed the name to shapefile instead of shape, just so to not be confused with a df property.
