Skip to content

Instantly share code, notes, and snippets.

@Kirkman
Created November 19, 2025 19:32
Show Gist options
  • Select an option

  • Save Kirkman/47bbf2dbcf68894f3df053f5f1951188 to your computer and use it in GitHub Desktop.

Select an option

Save Kirkman/47bbf2dbcf68894f3df053f5f1951188 to your computer and use it in GitHub Desktop.
ConcatField - Aggregation for Python's Agate library, to concatenate every value in a column into a single string
class ConcatField(agate.Aggregation):
"""
Concatenate every value in a column into a string.
:param column_name:
The name of any column.
"""
def __init__(self, column_name, sep='|'):
self._column_name = column_name
self._sep = sep
def get_aggregate_data_type(self, table):
return agate.Text()
def validate(self, table):
pass
# column = table.columns[self._column_name]
# if not isinstance(column.data_type, Text):
# raise DataTypeError('MaxLength can only be applied to columns containing Text data.')
def run(self, table):
"""
:returns:
:class:`str`.
"""
column = table.columns[self._column_name]
values = [str(v) if v != None else '' for v in column.values()]
s = self._sep.join(values)
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment