@@ -136,26 +136,33 @@ with each row having False in all but one column (True).
136
136
These are called `dummy variables <https://en.wikipedia.org/wiki/Dummy_variable_(statistics) >`_, or one-hot encoding.
137
137
:class: `pandas.Categorical ` objects can easily be converted to and from such an encoding.
138
138
139
+ :meth: `pandas.Categorical.get_dummies ` produces a dataframe of dummy variables.
140
+ It works in the same way and supports most of the same arguments as :func: `pandas.get_dummies `.
141
+
142
+ .. ipython :: python
143
+
144
+ cat = pd.Categorical([" a" , " b" , " b" , " c" ])
145
+ cat
146
+
147
+ cat.get_dummies()
148
+
139
149
The :meth: `pandas.Categorical.from_dummies ` class method accepts a dataframe
140
150
whose dtypes are coercible to boolean, and an ``ordered `` argument
141
151
for whether the resulting ``Categorical `` should be considered ordered
142
152
(like the ``Categorical `` constructor).
143
153
A column with a NA index will be ignored.
144
154
Any row which is entirely falsey, or has a missing value,
145
155
will be uncategorised.
146
-
147
- :meth: `pandas.Categorical.get_dummies ` produces a dataframe of dummy variables.
148
- It works in the same way and supports most of the same arguments as :func: ` pandas.get_dummies ` .
156
+ In the same way that :func: ` pandas.get_dummies ` can add a prefix to string category names,
157
+ :meth: `~ pandas.Categorical.from_dummies ` can filter a dataframe for columns with a prefix:
158
+ the resulting `` Categorical `` will have the prefix stripped from its categories .
149
159
150
160
.. ipython :: python
151
161
152
- cat = pd.Categorical([" a" , " b" , " b" , " c" ])
153
- cat
154
-
155
- dummies = cat.get_dummies()
162
+ dummies = pd.get_dummies([" a" , " b" , " b" , " c" ], prefix = " cat" )
156
163
dummies
157
164
158
- pd.Categorical.from_dummies(dummies)
165
+ pd.Categorical.from_dummies(dummies, prefix = " cat " )
159
166
160
167
161
168
.. versionadded :: 1.2.0
0 commit comments