pandas.Series.str.get_dummies

pandas.Series.str.get_dummies拆分series中以"|"分隔的字符串,然后返回一个DataFrame对象。

Parameters:

  • sep: str, default “|”. String to split on.

Returns:

  • DataFrame. Dummy variables corresponding to values of the Series.

举例:

import pandas as pd


genres = pd.Series(['Adventure|Animation|Children|Comedy|Fantasy',
                    'Acventure|Children|Fantasy', 'Comedy|Romance',
                    'Comedy', 'Action|Crime|Thriller', 'Comedy|Romance',
                    'Adventure|Children', 'Action',
                    'Action|Adventure|Thriller', 'Comedy|Drama|Romance',
                    'Comedy|Horror', 'Adventure|Animation|Children', 'Drama',
                    'Action|Adventure|Romance', 'Drama|Romance', 'Comedy',
                    'Comedy', 'Action|Comedy|Crime|Drama|Thriller',
                    'Comedy|Crime|Thriller'])
df = genres.str.get_dummies()

genres:
pandas.Series.str.get_dummies_第1张图片
df:
pandas.Series.str.get_dummies_第2张图片

你可能感兴趣的:(学习笔记)