Return DataArray of dummy/indicator variables. Each string in the DataArray is split at `sep`. A new dimension is created with coordinates for each unique result, and the corresponding element of that dimension is `True` if that result is present and `False`
(
self,
dim: Hashable,
sep: str | bytes | Any = "|",
)
| 2768 | ) |
| 2769 | |
| 2770 | def get_dummies( |
| 2771 | self, |
| 2772 | dim: Hashable, |
| 2773 | sep: str | bytes | Any = "|", |
| 2774 | ) -> DataArray: |
| 2775 | """ |
| 2776 | Return DataArray of dummy/indicator variables. |
| 2777 | |
| 2778 | Each string in the DataArray is split at `sep`. |
| 2779 | A new dimension is created with coordinates for each unique result, |
| 2780 | and the corresponding element of that dimension is `True` if |
| 2781 | that result is present and `False` if not. |
| 2782 | |
| 2783 | If `sep` is array-like, it is broadcast against the array and applied |
| 2784 | elementwise. |
| 2785 | |
| 2786 | Parameters |
| 2787 | ---------- |
| 2788 | dim : hashable |
| 2789 | Name for the dimension to place the results in. |
| 2790 | sep : str, default: "|". |
| 2791 | String to split on. |
| 2792 | If array-like, it is broadcast. |
| 2793 | |
| 2794 | Returns |
| 2795 | ------- |
| 2796 | dummies : array of bool |
| 2797 | |
| 2798 | Examples |
| 2799 | -------- |
| 2800 | Create a string array |
| 2801 | |
| 2802 | >>> values = xr.DataArray( |
| 2803 | ... [ |
| 2804 | ... ["a|ab~abc|abc", "ab", "a||abc|abcd"], |
| 2805 | ... ["abcd|ab|a", "abc|ab~abc", "|a"], |
| 2806 | ... ], |
| 2807 | ... dims=["X", "Y"], |
| 2808 | ... ) |
| 2809 | |
| 2810 | Extract dummy values |
| 2811 | |
| 2812 | >>> values.str.get_dummies(dim="dummies") |
| 2813 | <xarray.DataArray (X: 2, Y: 3, dummies: 5)> Size: 30B |
| 2814 | array([[[ True, False, True, False, True], |
| 2815 | [False, True, False, False, False], |
| 2816 | [ True, False, True, True, False]], |
| 2817 | <BLANKLINE> |
| 2818 | [[ True, True, False, True, False], |
| 2819 | [False, False, True, False, True], |
| 2820 | [ True, False, False, False, False]]]) |
| 2821 | Coordinates: |
| 2822 | * dummies (dummies) <U6 120B 'a' 'ab' 'abc' 'abcd' 'ab~abc' |
| 2823 | Dimensions without coordinates: X, Y |
| 2824 | |
| 2825 | See Also |
| 2826 | -------- |
| 2827 | pandas.Series.str.get_dummies |