2

This question has been asked before but I couldn't get the solution to work. I have a shapefile with lots of empty columns and I would like to remove them all with a script or some tools.

This is the script that was posted as the solution to the other thread.

lyr = iface.activeLayer()
lstDelete = []

for idx in lyr.dataProvider().attributeIndexes():
    uv = lyr.dataProvider().uniqueValues( idx )
    if not uv:
        lstDelete.append( idx )

lyr.dataProvider().deleteAttributes( lstDelete )

# Update the layer structure
lyr.updateFields()

When I try it both on Windows 2.18.1 and Mac 2.18.0 I get no change to my active layer.

2
  • That script is quite odd - it deletes attributes from a layer ONLY when they have at least one attribute value present.
    – ndawson
    Jan 18, 2017 at 23:08
  • 3
    Author of answer to original question has updated answer that now works for me gis.stackexchange.com/questions/113471/…
    – jfact0ry
    Jan 18, 2017 at 23:32

3 Answers 3

4

if you're using qgis 2.18.1, use save as.., and the select fields to export.. option.

this is a fairly recent addition, I think. I find it very useful with Openstreetmap data, which often has lots of empty columns.

it's also a lot faster than deleting columns from the attribute viewer, which was painfully slow with hundreds of columns :)

0
2

I don't know how to do it by script but it can be done fairly easily by deleting multiple columns at a time, one layer at a time through the interface:

  • Start a new QGis scrap project to work in and load all layers from the shape file.
  • Select the first layer in the layer panel and open its attribute table.
  • Enable edit mode (Ctrl+E)
  • If you already have a list of the columns you want to delete, you can select "Delete Field" (Ctrl+L), highlight the ones you want to delete, then hit OK.
  • If you need to figure out which columns are empty you can click on each column header to sort by that column, then check top and bottom of each column. If the fields are empty at top and bottom they will be empty everywhere else.
  • You can delete them as you go or you can jot the empty column names down in notepad as you go then delete them all at once using "Delete field" as described above.
  • When you've deleted all empty columns for the layer, save the edits, close the table and repeat the process with the rest of the layers.

Admittedly not as fast as a script but quite possibly faster than figuring out how to write the script.

I hope that helps.

0

Another option (if you're using the GUI) is to double click on the layer from the layer panel. Then click on the Fields menu, and click the pen which toggles editing (*). This should allow you to remove fields by pointing and selecting.

*If you cannot toggle editing, try re-saving the layer as a new shapefile so that you can gain editing privileges.

I hope this helps!

1
  • This is something I've used before but I had many fields and I just wanted to wipe out the empty ones with one script.
    – jfact0ry
    Jan 23, 2017 at 18:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.