【问题标题】:Create dummies for non-unique lists into column in Python在 Python 中为非唯一列表创建假人到列中
【发布时间】:2016-12-02 18:30:35
【问题描述】:

目前我有下一个数据框:

import pandas as pd
df= pd.DataFrame({"ID" : ['1','2','3','4','5'], 
                     "col2" : [['a', 'b', 'c'], 
                               ['c', 'd', 'e', 'f'], 
                               ['f', 'b', 'f'], 
                               ['a', 'c', 'b'], 
                               ['b', 'a', 'b']]})

print(df)
  ID          col2
0  1     [a, b, c]
1  2  [c, d, e, f]
2  3     [f, b, f]
3  4     [a, c, b]
4  5     [b, a, d]

我想为 col2 创建一个带有假人的新数据框,如下所示:

    ID   a   b   c   d   e   f
0   1    1   1   1   0   0   0
1   2    0   0   1   1   1   1
2   3    0   1   0   0   0   1
3   4    1   1   1   0   0   0
4   5    1   1   0   1   0   0

使用以下代码为列列表中的每个字母生成不同的列:

df2= df.col2.str.get_dummies(sep = ",")
pd.concat([data['col1'], df], axis=1)

ID  a   b   b]  c   c]  d   d]  e   f]  [a [b  [c  [f
1   0   1   0   0   1   0   0   0   0   1   0   0   0
2   0   0   0   0   0   1   0   1   1   0   0   1   0
3   0   1   0   0   0   0   0   0   1   0   0   0   1
4   0   0   1   1   0   0   0   0   0   1   0   0   0
5   1   0   0   0   0   0   1   0   0   0   1   0   0

使用下面的代码,根据它们所在的位置,为列列表中的每个字母生成不同的列。你们中有人知道为什么会经历这个吗? pd.get_dummies 选项也不起作用。

【问题讨论】:

    标签: python python-2.7 pandas


    【解决方案1】:

    str.get_dummies 在字符串上效果很好,因此您可以将列表转换为某物分隔的字符串并在该字符串上使用 str_get_dummies。例如,

    df['col2'].str.join('@').str.get_dummies('@')
    Out: 
       a  b  c  d  e  f
    0  1  1  1  0  0  0
    1  0  0  1  1  1  1
    2  0  1  0  0  0  1
    3  1  1  1  0  0  0
    4  1  1  0  0  0  0
    

    这里,@ 是一个没有出现在列表中的任意字符。

    然后,你可以像往常一样连接:

    pd.concat([df['ID'], df['col2'].str.join('@').str.get_dummies('@')], axis=1)
    Out: 
      ID  a  b  c  d  e  f
    0  1  1  1  1  0  0  0
    1  2  0  0  1  1  1  1
    2  3  0  1  0  0  0  1
    3  4  1  1  1  0  0  0
    4  5  1  1  0  0  0  0
    

    【讨论】:

      【解决方案2】:

      使用理解字典可能会更快

      In [40]: pd.DataFrame({k: 1 for k in x} for x in df.col2.values).fillna(0).astype(int)
      Out[40]:
         a  b  c  d  e  f
      0  1  1  1  0  0  0
      1  0  0  1  1  1  1
      2  0  1  0  0  0  1
      3  1  1  1  0  0  0
      4  1  1  0  0  0  0    
      
      In [48]: pd.concat([
                      df['ID'], 
                      pd.DataFrame({k: 1 for k in x} for x in df.col2).fillna(0).astype(int)],
                  axis=1)
      Out[48]:
        ID  a  b  c  d  e  f
      0  1  1  1  1  0  0  0
      1  2  0  0  1  1  1  1
      2  3  0  1  0  0  0  1
      3  4  1  1  1  0  0  0
      4  5  1  1  0  0  0  0
      

      时间安排

      In [2942]: df.shape
      Out[2942]: (50000, 2)
      
      In [2945]: %timeit pd.DataFrame({k: 1 for k in x} for x in df.col2).fillna(0).astype(int)
      10 loops, best of 3: 137 ms per loop
      
      In [2946]: %timeit df['col2'].str.join('@').str.get_dummies('@')
      1 loop, best of 3: 395 ms per loop
      

      【讨论】:

      • 这太棒了!它速度更快,内存效率更高!
      【解决方案3】:

      使用您提供的 df... 这很好用

      def f1(x):
          # 1 if exist
          return pd.Series(1, set(x))
      
      def f2(x):
          # count occurences
          return pd.value_counts(x)
      
      print(df.set_index('ID').col2.apply(f1).fillna(0).astype(int).reset_index())
      print('')
      print(df.set_index('ID').col2.apply(f2).fillna(0).astype(int).reset_index())
      
        ID  a  b  c  d  e  f
      0  1  1  1  1  0  0  0
      1  2  0  0  1  1  1  1
      2  3  0  1  0  0  0  1
      3  4  1  1  1  0  0  0
      4  5  1  1  0  0  0  0
      
        ID  a  b  c  d  e  f
      0  1  1  1  1  0  0  0
      1  2  0  0  1  1  1  1
      2  3  0  1  0  0  0  2
      3  4  1  1  1  0  0  0
      4  5  1  2  0  0  0  0
      

      【讨论】:

      • 您的问题是,为了从 str 访问器中使用 get_dummies,您将其解析为导致整个问题的字符串。你左转了三个右转。这对我来说更直观。但是,str 是矢量化的,将具有一些性能优势。无论如何,很高兴我能帮上忙
      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2020-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2012-08-22
      • 2013-01-27
      • 2016-05-15
      相关资源
      最近更新 更多