5

I have 11 shapefiles that shows administrative areas for 11 different countries. For each country, I have 448 columns in the attribute table that records average rainfall data. They look like this:

enter image description here

I would like to change the header of each column displayed here. So "RFEb1_me_1" will be renamed to "JAN1983", "RFEb2_me_2" will be renamed to "FEB1983"...and all the way to the 448th column, "APR2020". I am planning on making the same changes to all 11 attribute tables.

How can I make this happen using QGIS (PyQGIS)?

In answer to @Kadir's question, here's a screenshot of some of my last columns.

enter image description here

7
  • What is the name of 448th column? I ask becuase of 10-character limit of field name in shapefiles. Nov 21, 2021 at 8:16
  • 2
    In your pattern, 448th column is APR2020. Nov 21, 2021 at 9:12
  • 5
    I wonder how it is possible to have 448 columns in shapefile. ESRI claims that there is a limit at 255 support.esri.com/en/technical-article/000007920.
    – user30184
    Nov 21, 2021 at 11:18
  • -Hello Kadir. Thank you so much for answering the question! I have updated my original question to show the 448th column. And yes, 448th is APR2020. My mistakes!
    – Zhiwei Jin
    Nov 21, 2021 at 14:31
  • -Hello 30184. Please see my updated question! Thank you so much!
    – Zhiwei Jin
    Nov 21, 2021 at 14:32

2 Answers 2

10

Use this script. As a precaution, backup the shapefile first.

#generate monthyear
months = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
years = range(1983, 2022)
month_year = [f"{month}{year}" for year in years for month in months]
# month_year = ["JAN1983", "FEB1983", ... , "DEC2021"]
    
layer = iface.activeLayer()
layer.startEditing()

for field in layer.fields():
    # n = split("_")  # returns ["RFEb12", "m", "1"]
    # n = n[0].split("b") # returns ["RFE", "12"]
    # n = int(n[1]) # 12
    #
    # "RFEb12_m_1" -> "DEC1983"

    if not field.name().startswith('RFEb'):
        continue
    
    n = int(field.name().split("_")[0].split("b")[1])

    idx = layer.fields().indexFromName(field.name())
    layer.renameAttribute(idx, month_year[n-1])
        
layer.commitChanges()
5
  • Hi Kadir. This looks absolutely amazing. May I please ask if this will work if the attribute table contains other columns that does not start with the RFEbx_mean header. I have other attributes in the table that I do not want to change the name.
    – Zhiwei Jin
    Nov 21, 2021 at 14:33
  • Maybe checking if field name starts with REFb may work. Updated the question. Nov 21, 2021 at 16:26
  • Hi Kadir. Much thanks to your edits! Prior to using your python script, do I have to insert any command to specify which layer (Attribute table) I will be making the changes to? Or does your script already contains this. I am so sorry but I am completely new to Python and this is perhaps the only way to get things done?
    – Zhiwei Jin
    Nov 21, 2021 at 17:06
  • You don’t need to specify any layer. Highlight/select the layer you want to change in Layers panel and run the script in QGIS Python Editor. layer = iface.activeLayer() gives you highlighted/selected layer. Nov 21, 2021 at 18:18
  • Hi Kadir. Your codes works just perfectly fine with all my layers. Thank you soooooooo much! You are the savior of the day!
    – Zhiwei Jin
    Nov 21, 2021 at 21:57
3

I would create a clear new layer, with the correct attribute names. And then use the plugin « Append features to layer », to select which column goes where ! Or export the table to Excel with the ID, change what you need in Excel, reload the table and make a join with the ID…

3
  • 1
    Excel is extremely dangerous to shapefiles, since it routinely mangles the dBase records, changing the row count or row order, scrambling the positional linkage between geometries and their attributes. Answers that recommend the use of Excel on dBase files should contain a warning about potential data corruption.
    – Vince
    Nov 21, 2021 at 16:07
  • Not if you use it as a join table!
    – katagena
    Nov 21, 2021 at 16:31
  • And using Python is much more dangerous for potential data corruption if you don’t know what you are doing ;-)
    – katagena
    Nov 21, 2021 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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