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:
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.
-
What is the name of 448th column? I ask becuase of 10-character limit of field name in shapefiles.Kadir Şahbaz– Kadir Şahbaz2021年11月21日 08:16:53 +00:00Commented Nov 21, 2021 at 8:16
-
2In your pattern, 448th column is APR2020.Kadir Şahbaz– Kadir Şahbaz2021年11月21日 09:12:03 +00:00Commented Nov 21, 2021 at 9:12
-
5I 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– user301842021年11月21日 11:18:44 +00:00Commented 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– Zhiwei Jin2021年11月21日 14:31:59 +00:00Commented Nov 21, 2021 at 14:31
-
-Hello 30184. Please see my updated question! Thank you so much!Zhiwei Jin– Zhiwei Jin2021年11月21日 14:32:45 +00:00Commented Nov 21, 2021 at 14:32
2 Answers 2
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()
-
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– Zhiwei Jin2021年11月21日 14:33:00 +00:00Commented Nov 21, 2021 at 14:33
-
Maybe checking if field name starts with
REFb
may work. Updated the question.Kadir Şahbaz– Kadir Şahbaz2021年11月21日 16:26:00 +00:00Commented 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– Zhiwei Jin2021年11月21日 17:06:19 +00:00Commented 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.Kadir Şahbaz– Kadir Şahbaz2021年11月21日 18:18:43 +00:00Commented 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– Zhiwei Jin2021年11月21日 21:57:28 +00:00Commented Nov 21, 2021 at 21:57
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...
-
1Excel 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– Vince2021年11月21日 16:07:51 +00:00Commented Nov 21, 2021 at 16:07
-
Not if you use it as a join table!katagena– katagena2021年11月21日 16:31:14 +00:00Commented 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– katagena2021年11月21日 16:34:06 +00:00Commented Nov 21, 2021 at 16:34
Explore related questions
See similar questions with these tags.