Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created November 16, 2011 22:37
Show Gist options
  • Save taldcroft/1371713 to your computer and use it in GitHub Desktop.
Save taldcroft/1371713 to your computer and use it in GitHub Desktop.
Select and possibly re-order columns in a structured array
import asciitable
def structured_array(arr, colnames):
"""Select and possibly re-order columns from a structured array.
:param arr: numpy structured array
:param colnames: column names to include
"""
dtypes = [(x, arr[x].dtype, arr[x].shape[1:]) for x in colnames]
dat = np.ndarray(len(arr), dtype=dtypes)
for colname in colnames:
dat[colname] = arr[colname]
return dat
d1 = asciitable.read(['a b c', '1 2 3', '4 5 6'])
d2 = structured_array(d1, ['c', 'a'])
asciitable.write(d2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment