Skip to content

Instantly share code, notes, and snippets.

@kwmiebach
Last active August 2, 2023 15:56
Show Gist options
  • Save kwmiebach/362a41b0fbf0cad6db36a1e037bcfbe6 to your computer and use it in GitHub Desktop.
Save kwmiebach/362a41b0fbf0cad6db36a1e037bcfbe6 to your computer and use it in GitHub Desktop.
add a 1-based seqence column to a petl table
'''
Versioned here:
https://gist.github.com/kwmiebach/362a41b0fbf0cad6db36a1e037bcfbe6
'''
def add_seq(tab,name='seq',position=0):
# NO, PETL IS CURRENTLY BROKEN - depending on python version
# tab = ETL.addrownumbers(tab, 'seq')
import petl as ETL
inner = _add_seq_gen(tab,name,position)
return ETL.wrap(inner)
def _add_seq_gen(tab,name='seq',position=0):
# Adds a new column with the given name
# default name: 'seq' at the given position
# (default: most left position).
# The content is a 1 based sequence number.
# You can use negative position to count from the right.
# Make it iterable:
itab = iter(tab)
# Get first row (header)
hd_list = next(itab, None)
if hd_list is None:
# empty table -> return an empty table
# this is done by not yielding anything. hopefully this is ok.
return
# insert name at position
hd_list = list(hd_list)
hd_count = len(hd_list)
# position = 0 means most left position
# fix negative position or position > hd_count
if position < 0:
position = hd_count + position + 1
if position < 0:
position = 0
if position > hd_count:
position = hd_count
hd_list.insert(position, name)
yield tuple(hd_list)
# all remaining rows:
for idx, row in enumerate(itab, 1): # start enumerate from 1 as you want 1-based sequence
row_as_list = list(row)
row_as_list.insert(position, idx)
yield tuple(row_as_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment