简体   繁体   English

从两个值的总和制作虚拟列的pythonic方法

[英]pythonic way of making dummy column from sum of two values

I have a dataframe with one column called label which has the values [0,1,2,3,4,5,6,8,9] .我有一个名为label列的数据框,其值为[0,1,2,3,4,5,6,8,9] I would like to make dummy columns out of this, but I would like some labels to be joined together, so for example I want dummy_012 to be 1 if the observation has either label 0, 1 or 2.我想从中制作虚拟列,但我希望将一些标签连接在一起,因此例如,如果观察值具有标签 0、1 或 2,我希望dummy_012为 1。

If i use the command df2 = pd.get_dummies(df, columns=['label']) , it would create 9 columns, 1 for each label.如果我使用命令df2 = pd.get_dummies(df, columns=['label']) ,它将创建 9 列,每个标签 1 列。

I know I can use df2['dummy_012']=df2['dummy_0']+df2['dummy_1']+df2['dummy_2'] after that to turn it into one joint column, but I want to know if there's a more pythonic way of doing it (or some function where i can just change the parameters to the joins).我知道在那之后我可以使用df2['dummy_012']=df2['dummy_0']+df2['dummy_1']+df2['dummy_2']将它变成一个联合列,但我想知道是否有更多的pythonic方式(或一些我可以只更改连接参数的函数)。

Maybe this approach can give a idea:也许这种方法可以给出一个想法:

groups = ['012', '345', '6789']
for gp in groups:
    df.loc[df['Label'].isin([int(x) for x in gp]), 'Label_Group'] = f'dummies_{gp}'

Output:输出:

   Label   Label_Group
0      0   dummies_012
1      1   dummies_012
2      2   dummies_012
3      3   dummies_345
4      4   dummies_345
5      5   dummies_345
6      6  dummies_6789
7      8  dummies_6789
8      9  dummies_6789

And then apply dummy:然后应用虚拟:

df_dummies = pd.get_dummies(df['Label_Group'])
   dummies_012  dummies_345  dummies_6789
0            1            0             0
1            1            0             0
2            1            0             0
3            0            1             0
4            0            1             0
5            0            1             0
6            0            0             1
7            0            0             1
8            0            0             1

I don't know that this is pythonic because a more elegant solution might exist, but I does allow you to change parameters and it's vectorized.我不知道这是 pythonic,因为可能存在更优雅的解决方案,但我确实允许您更改参数并且它是矢量化的。 I've read that get_dummies() can be a bit slow with large amounts of data and vectorizing pandas is good practice in general.我读过 get_dummies() 在处理大量数据时可能会有点慢,而矢量化熊猫通常是一种很好的做法。 So I vectorized this function and had it do its calculations with numpy arrays.所以我对这个函数进行了矢量化,并让它用 numpy 数组进行计算。 It should give you a boost in performance as the dataset increases in size compared to similar functions.与类似函数相比,随着数据集大小的增加,它应该会提高性能。

This function will take your dataframe and a list of numbers as strings and will return your dataframe with the column you wanted.此函数将您的数据框和数字列表作为字符串,并将返回您想要的列的数据框。

def get_dummy(df,column_nos):
    new_col_name = 'dummy_'+''.join([i for i in column_nos])
    vector_sum = sum([df[i].values for i in column_nos])
    df[new_col_name] = [1 if i>0 else 0 for i in vector_sum]

    return df

In case you'd rather the input to be integers rather than strings, you can tweak the above function to look like below.如果您希望输入是整数而不是字符串,您可以将上述函数调整为如下所示。

def get_dummy(df,column_nos):
    column_names = ['dummy_'+str(i) for i in column_nos]
    new_col_name = 'dummy_'+''.join([str(i) for i in sorted(column_nos)])

    vector_sum = sum([df[i].values for i in column_names])
    df[new_col_name] = [1 if i>0 else 0 for i in vector_sum]

    return df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM