w3resource

Pandas Series: str.get_dummies() function

Series-str.get_dummies() function

The str.get_dummies() function is used to split each string in the Series by sep and return a DataFrame of dummy/indicator variables.

Syntax:

Series.str.get_dummies(self, sep='|')
Pandas Series: str.get_dummies() function

Parameters:

Name Description Type/Default Value Required / Optional
sep String to split on. str, default “|” Required

Returns: DataFrame Dummy variables corresponding to values of the Series.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series(['p|q', 'p', 'p|r']).str.get_dummies()

Output:

  p	q	r
0	1	1	0
1	1	0	0
2	1	0	1

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series(['p|q', np.nan, 'p|r']).str.get_dummies()

Output:

  p	q	r
0	1	1	0
1	0	0	0
2	1	0	1
Pandas Series: str.get_dummies() function

Previous: Series-str.isdecimal() function
Next: Series-cat.rename_categories() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-str-get_dummies.php