swap_cols
- pyhelpers.ops.swap_cols(array, c1, c2, as_list=False)
Swap positions of two columns in an array.
- Parameters
array (numpy.ndarray) – an array
c1 (int) – index of a column
c2 (int) – index of another column
as_list (bool) – whether to return a list
- Returns
a new array/list in which the positions of the c1-th and c2-th columns are swapped
- Return type
numpy.ndarray or list
Examples:
>>> from pyhelpers.ops import swap_cols >>> from pyhelpers._cache import example_dataframe >>> example_arr = example_dataframe().to_numpy(dtype=int) >>> example_arr array([[530034, 180381], [406689, 286822], [383819, 398052], [582044, 152953]]) >>> # Swap the 0th and 1st columns >>> new_arr = swap_cols(example_arr, c1=0, c2=1) >>> new_arr array([[180381, 530034], [286822, 406689], [398052, 383819], [152953, 582044]]) >>> new_list = swap_cols(example_arr, c1=0, c2=1, as_list=True) >>> new_list [[180381, 530034], [286822, 406689], [398052, 383819], [152953, 582044]]