Skip to main content

I’m using the pyairtable python package, and I’m looking for a way to delete columns in a table and reorder columns based on how they are named (e.g. if the columns are dates, I want the most recent date to be the left most column).

 

Not sure if there is a way to do this using python, but I’m hoping I wont have to do this manually.

 

Thanks in advance!

That is not possible, because of 2 reasons:

(a) In a database, there is no inherent ordering to the columns. They are just fields that can be manually rearranged on different views & interfaces in different orders for cosmetic purposes, but there is no inherent ordering. This is different than what you are accustomed to doing on a spreadsheet, which acts differently than a database. 

(b) In a database, columns should not typically be used for multiple dates. That might be something that you would see in a spreadsheet, but you wouldn’t usually see that in a database.

In a database, you would create a linked record field that links to all of your date records in another table.

So, instead of each date being a column, each date would be a row in another table.

Then, you can sort your dates in that other table, or you can create sorted lookup fields back in your original table. You can also delete your dates in that other table as well.

But everything would hinge on you creating the linked record to that other table of dates first.

Since you’re new to Airtable, you might benefit from my free Airtable training course, which you can take for free by signing up for a free 30-day trial with LinkedIn Learning.

My course is a bit outdated, because it was created many years ago, but the core concepts of database structure, linked record fields, and lookup fields still remain.

Hope this helps! If you’d like to hire the best Airtable consultant to help you with anything Airtable-related, please feel free to contact me through my website: Airtable consultant — ScottWorld


There’s no API endpoint for deleting a field or updating the field order in a view and so I doubt pyairtable can do those things I’m afraid


Hey ​@ncl1805,

For further reference (and future readers) -following with Adam’s answer above- you can check out Airable’s full API documentation here

Hope this helps.

Mike, Consultant @ Automatic Nation


Hi,
You can’t change the order of fields with API calls or code.
But to avoid manual way to do it, you can filter view to exclude all records, download as csv (only line with comma-separated field names will be created), sort it, then upload as csv to a new table. 
Then you can do the same with unfiltered view and upload to this new_sorted_table, it will map all respective fields and their data according to the new order.

The question is how to sort field names? It depends on format. ISO dates can be sorted alphabetically,
so I would use something like 

console.log(base.getTable('TABLE_NAME').
fields.map(f=>f.name).sort().reverse().join(','))

For other formats I would paste list of values into new temporary linked field to a new table (temporary as well). It can be used to transpose list of values into column of values (capable to be converted to dates and sorted) and vice versa. Unfortunately, it needs much more time to explain than to do it.

P.S. I’m totally agree with ​@ScottWorld , you should reformat your data. Further use of table with such unusual design will be eternal source of problems.


Reply