代码之家  ›  专栏  ›  技术社区  ›  KubiK888

基于另一列迭代地将值分配给pandas列

  •  0
  • KubiK888  · 技术社区  · 7 年前

    我在pandas数据框中有一个名为“label”的变量,它包含多个字符串值(例如: 'label1', "label2', 'label3'... )

    label
    label1
    label1
    label23
    label3
    label11
    

    我将所有唯一值输出到一个列表中,然后创建新变量

    unique_labels = df['label'].unique()
    
    for i in unique_labels: # create new single label variable holders
        df[str(i)] = 0
    

    现在我有

    label    label1    label2 .... label23
    label1     0         0            0
    label23    0         0            0
    

    我想根据 'label' 在新的单标签变量上,如下所示

    label    label1    label2 .... label23
    label1     1         0            0
    label23    0         0            1
    

    这是我的密码

    def single_label(df):
    for i in range(len(unique_labels)):
        if df['label'] == str(unique_labels[i]):
            df[unique_labels[i]] == 1
    
    
    df = df.applymap(single_label)
    

    得到这个错误

    TypeError: ("'int' object is not subscriptable", 'occurred at index Unnamed: 0')
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   sacuL    7 年前

    IIUC,你可以用 pd.get_dummies ,在删除重复项之后,这将比以迭代方式执行此操作更快并产生更干净的代码:

    df.drop_duplicates().join(pd.get_dummies(df.drop_duplicates()))
    
         label  label_label1  label_label11  label_label23  label_label3
    0   label1             1              0              0             0
    2  label23             0              0              1             0
    3   label3             0              0              0             1
    4  label11             0              1              0             0
    

    你可以去掉那些 label 前缀和下划线使用 prefix prefix_sep 论据:

    df.drop_duplicates().join(pd.get_dummies(df.drop_duplicates(),
                                             prefix='', prefix_sep=''))
    
         label  label1  label11  label23  label3
    0   label1       1        0        0       0
    2  label23       0        0        1       0
    3   label3       0        0        0       1
    4  label11       0        1        0       0
    

    编辑 :第二列,即:

    >>> df
         label second_column
    0   label1             a
    1   label1             b
    2  label23             c
    3   label3             d
    4  label11             e
    

    只要打电话 警察去拿假人 仅在“标签”列上:

    df.drop_duplicates('label').join(pd.get_dummies(df['label'].drop_duplicates(),
                                             prefix='', prefix_sep=''))
    
         label second_column  label1  label11  label23  label3
    0   label1             a       1        0        0       0
    2  label23             c       0        0        1       0
    3   label3             d       0        0        0       1
    4  label11             e       0        1        0       0
    

    但是你要去掉没有重复的行,我认为这不是你想要的(除非我错了)。如果没有,请忽略drop duplicates调用:

    df.join(pd.get_dummies(df['label'], prefix='', prefix_sep=''))
    
         label second_column  label1  label11  label23  label3
    0   label1             a       1        0        0       0
    1   label1             b       1        0        0       0
    2  label23             c       0        0        1       0
    3   label3             d       0        0        0       1
    4  label11             e       0        1        0       0