【问题标题】:Converting single column with strings with separator ('|') to multiple columns with binary values based on the string value将带有分隔符 ('|') 的字符串的单列转换为基于字符串值的具有二进制值的多列
【发布时间】:2020-10-08 03:16:55
【问题描述】:

我有一百万条记录的数据框有一列,其中包含多个以分隔符作为分隔符的组合字符串。

在所需的数据框中,我需要保留该列并有多个列托管分隔的字符串作为列标题,并根据行中可用的组合使用二进制值。

这需要与其他功能结合以提供模型估计器。

附上数据样本以供参考。

x.head(20)
Genres
793754  Drama|Sci-Fi
974374  Drama|Romance
950027  Horror|Sci-Fi
998553  Comedy
757593  Action|Thriller
943002  Comedy|Romance
699895  Drama|Romance
228740  Animation|Comedy|Thriller
365470  Comedy
174365  Comedy|Fantasy
827401  Drama
75922   Comedy|Drama
934548  Animation|Children's|Comedy|Musical|Romance
281451  Comedy|Sci-Fi
694344  Sci-Fi
731063  Action|Adventure
978029  Animation|Comedy
283943  Drama|Sci-Fi|Thriller
961082  Action|Adventure|Fantasy|Sci-Fi
778922  Action|Crime|Romance

所需的列(18 列)从具有唯一功能的整个数据中提取为列表,并根据行字符串数据填充二进制 0 或 1。

genre_movies=list(genre_movies.stack().unique())
genre_movies
['Drama',
 'Animation',
 "Children's",
 'Musical',
 'Romance',
 'Comedy',
 'Action',
 'Adventure',
 'Fantasy',
 'Sci-Fi',
 'War',
 'Thriller',
 'Crime',
 'Mystery',
 'Western',
 'Horror',
 'Film-Noir',
 'Documentary']

我是 Pandas 的新手,感谢您的帮助。

【问题讨论】:

    标签: python pandas string dataframe multiple-columns


    【解决方案1】:

    请检查这是否是您想要的: (我必须手动输入类型,所以我只放了 3 行)

                   Genres  Drama  Sci-Fi  Romance  Horror
    793754   Drama|Sci-Fi   True    True    False   False
    974374  Drama|Romance   True   False     True   False
    950027  Horror|Sci-Fi  False    True    False    True
    

    代码是:

    import pandas as pd 
    df = pd.DataFrame( {
                        'Genres' : ['Drama|Sci-Fi', 'Drama|Romance' , 'Horror|Sci-Fi']
                        },
                        index = [793754, 974374, 950027] , 
                        )
    genre_movies=list(df.Genres.unique())
    genre_movies2  = [words for segments in genre_movies for words in segments.split('|')]
    # get a list of unique genres
    
    for genre in genre_movies2:
        df[genre] = df.Genres.str.contains(genre, regex=False)
    

    @Ins_hunter 建议的方法 2 使用.get_dummies()方法

    df2 = df.Genres.str.get_dummies(sep='|')
    
    Action  Adventure   Animation   Children's  Comedy  Crime   Documentary Drama   Fantasy Film-Noir   Horror  Musical Mystery Romance Sci-Fi  Thriller    War Western
    0   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
    1   0   0   1   1   0   0   0   0   0   0   0   1   0   0   0   0   0   0
    2   0   0   0   0   0   0   0   0   0   0   0   1   0   1   0   0   0   0
    3   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
    4   0   0   1   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0
    ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
    1000204 0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   0   0
    1000205 0   0   0   0   0   0   0   1   0   0   0   0   0   1   0   0   1   0
    1000206 0   0   0   0   1   0   0   1   0   0   0   0   0   0   0   0   0   0
    1000207 0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0
    1000208 0   0   0   1   0   0   0   1   1   0   0   0   0   0   1   0   0   0
    1000209 rows × 18 columns
    

    并且可以合并回原始数据

    df3 = pd.concat([df, df2], axis=1)
    

    【讨论】:

    • @fenixano 感谢您的回复。是的,我需要以相同的方式,但是作为 1 和 0,我们总共有 18 个这样的特征(所以 18 或 17 列)具有二进制值。完整的数据集有 100 万个观测值。
    • @fenixano - 我尝试使用现有生成的唯一值列表 -genre_movies 。代码如下。对于genre_movies中的流派:x['Genres']=x['Genres'].str.contains(genre,regex=False) 但出现如下错误。“AttributeError: Can only use .str accessor with string values!” .感谢您的指导,请结束...
    • 感谢您的回复。是的,我需要以相同的方式使用二进制值的 1 和 0(所以 18 或 17 列)。尝试使用我现有的生成的唯一值列表 -genre_movies 。代码如下。对于genre_movies中的流派:x['Genres']=x['Genres'].str.contains(genre,regex=False) 但出现如下错误。“AttributeError: Can only use .str accessor with string values!” .感谢您的指导,请结束...
    • @Ins_hunter 1,True等于1,False为0,你可以用同样的方法求和。如果您特别需要int 1和0,可以使用.astype(int) 2,您没有提供数据所以我无法获得18列,我手动输入4作为演示。 3、由于代码在for循环中,出现错误,不能单独运行。如果您可以提供至少一个我可以导入到 python 的数据帧剪辑,我可以直接修改代码以便您运行它。否则需要自己修改。我不知道您的数据框是否被命名为“x”或“genre_movies”或其他名称
    • @fenixano-感谢您的回复。更新了数据框的详细信息。结构在粘贴过程中变形,对此感到抱歉。尝试运行 KNN,但当我运行 y_pred=knn.predict(X_test) 语句时系统被挂起
    猜你喜欢
    • 2010-10-19
    • 2022-11-10
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2021-10-13
    • 2017-03-11
    • 1970-01-01
    相关资源
    最近更新 更多