【问题标题】:Convert this Word DataFrame into Zero One Matrix Format DataFrame in Python Pandas eliminates " "在 Python Pandas 中将此 Word DataFrame 转换为零一矩阵格式 DataFrame 消除了“”
【发布时间】:2017-10-30 20:23:38
【问题描述】:

想将user_Id和技能dataFrame矩阵转换成零一DataFrame矩阵格式的用户及其对应的技能

输入数据帧

     user_Id                        skills

 0     user1               "java, hdfs, hadoop"
 1     user2               "python, c++, c"
 2     user3               "hadoop, java, hdfs"
 3     user4               "html, java, php"
 4     user5               "hadoop, php, hdfs"

所需的输出数据帧

 user_Id       java  c   c++     hadoop  hdfs    python  html    php     

 user1         1     0   0       1       1       0       0       0
 user2         0     1   1       0       0       1       0       0
 user3        1     0   0       1       1       0       0       0
 user4         1     0   0       0       0       0       1       1
 user5         0     0   0       1       1       0       0       1

【问题讨论】:

    标签: python-2.7 pandas dataframe sklearn-pandas


    【解决方案1】:

    为我工作str.get_dummies + concat:

    df1 = df['skills'].str.get_dummies(', ')
    print (df1)
       c  c++  hadoop  hdfs  html  java  php  python
    0  0    0       1     1     0     1    0       0
    1  1    1       0     0     0     0    0       1
    2  0    0       1     1     0     1    0       0
    3  0    0       0     0     1     1    1       0
    4  0    0       1     1     0     0    1       0
    
    df = pd.concat([df['user_Id'], df1], axis=1)
    print (df)
      user_Id  c  c++  hadoop  hdfs  html  java  php  python
    0   user1  0    0       1     1     0     1    0       0
    1   user2  1    1       0     0     0     0    0       1
    2   user3  0    0       1     1     0     1    0       0
    3   user4  0    0       0     0     1     1    1       0
    4   user5  0    0       1     1     0     0    1       0
    

    编辑:

    如果没有space,,请使用:

    df1 = df['skills'].str.get_dummies(',')
    

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多