Created
November 16, 2011 22:37
-
-
Save taldcroft/1371713 to your computer and use it in GitHub Desktop.
Select and possibly re-order columns in a structured array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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