I'm using geopandas to read a geojson file and output shapefiles. The issue is I cannot figure out how to export single features within that shapefile - only the entire shapefile. Just for ref, I'm using google colab.
here's what I have so far
os.makedirs('/content/drive/MyDrive/shapes')
gdf = gpd.read_file('/content/sample_data/countries.geojson')
for num, row in gdf.iterrows():
key = row.city
fileName = key+".shp"
path = '/content/drive/MyDrive/shapes/'+fileName
os.makedirs('/content/drive/MyDrive/shapes/'+fileName)
os.chdir('/content/drive/MyDrive/shapes/'+fileName)
gdf.to_file(fileName) # need to do something like row to file here
this code will export a bunch of shapefiles of the original geojson file & name them by a certain key. I can't figure out how to loop through the individual features and make a shapefile for each.
Since I didn't have your shapefile, I am going to answer your question based on what you have told in your question. First of all, you should not save the gdf dataframe every time because you want to save the row into a file(I think each row represents a city and you want to have this city in a different shapefile that is named after the city name). So, what I suggest is:
os.makedirs('/content/drive/MyDrive/shapes')
gdf = gpd.read_file('/content/sample_data/countries.geojson')
for num, row in gdf.iterrows():
key = row.city
fileName = key+".shp"
path = '/content/drive/MyDrive/shapes/'+fileName
os.makedirs(path)
os.chdir('/content/drive/MyDrive/shapes/'+fileName)
gdf.iloc[num:num+1,:].to_file(path)
Note that, instead of saving in filename I saved the file into path because if it wasn't the case, the last few lines in your code would be for nothing!