在Python中设置给定二进制标志的计数变量(pandas dataframe)

时间:2016-03-08 07:51:25

标签: python pandas dataframe

我有一个数据框,其布局如下,,包括“flag_common”:

cat      flag_1   flag_2  flag_3   pop      state       year    flag_common
value1   1        0       0        1.5      Ohio        2000    1
value3   1        1       0        1.7      Ohio        2001    1
value2   1        1       0        3.6      Ohio        2002    1
value11  0        1       0        2.4      Nevada      2001    2
value5   0        0       0        2.9      Nevada      2002    0
value9   0        0       1        11.1     New York    2003    3
value13  0        0       0        23.4     New York    2004    0
value10  1        1       0        0.1      California  2009    1
value7   0        0       0        0.3      California  2010    0
value14  0        1       1        1.1      California  2009    2

列“flag_common”应该通过查看二进制标志并插入值1-3来设置,具体取决于标志是1还是0.当两个标志对同一行设置为1时,标志与最小数字被插入“flag_common”。这必须是动态的,能够将flag_1处理为“flag_n”。

我有点使用行迭代方法和for循环来解决它,但是我的数据非常大并且变得非常慢,所以我希望有一种“pythonic”方式来编写这个矢量化的方法。 / p>

数据框的代码如下:

df = pd.DataFrame({'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'New York', 'New York', 'California', 'California', 'California'],
                 'year' : [2000, 2001, 2002, 2001, 2002, 2003, 2004, 2009, 2010, 2009],
                 'pop' : [1.5, 1.7, 3.6, 2.4, 2.9, 11.1, 23.4, 0.1, 0.3, 1.1],
               'cat' : ['value1', 'value3', 'value2', 'value11', 'value5', 'value9', 'value13', 'value10', 'value7', 'value14'],
               'flag_1' : [1, 1,1,0,0,0,0,1,0,0],
               'flag_2' : [0, 1,1,1,0,0,0,1,0,1],
               'flag_3' : [0, 0, 0, 0,0,1,0,0,0, 1]
                })

感谢我提前提出任何想法和建议!

1 个答案:

答案 0 :(得分:1)

您可以在columnsflag_1flag_2列的子集中使用flag_3 0idxmax,然后通过列表理解{{3}查找位置}。

但所有0值的排名不是flag_1,而是#get min value of columns 'flag_1','flag_2','flag_3' print df[['flag_1','flag_2','flag_3']].idxmax(axis=1) 0 flag_1 1 flag_1 2 flag_1 3 flag_2 4 flag_1 5 flag_3 6 flag_1 7 flag_1 8 flag_1 9 flag_2 dtype: object #get position of flag print df.columns.get_loc('flag_1') 1 #get positions all flags flag = [df.columns.get_loc(k) for k in df[['flag_1','flag_2','flag_3']].idxmax(axis=1)] print flag [1, 1, 1, 2, 1, 3, 1, 1, 1, 2] #alternative solution for positions of flags - last digit has to be number print [int(x[-1]) for x in df[['flag_1','flag_2','flag_3']].idxmax(axis=1)] [1, 1, 1, 2, 1, 3, 1, 1, 1, 2] 。因此,请使用get_loc进行更正。

#if all values in 'flag_1','flag_2','flag_3' are 0, get 0 else flag
df['new'] = np.where((df[['flag_1','flag_2','flag_3']].sum(axis=1)) == 0, 0, flag)
print df
       cat  flag_1  flag_2  flag_3   pop       state  year  flag_common  new
0   value1       1       0       0   1.5        Ohio  2000            1    1
1   value3       1       1       0   1.7        Ohio  2001            1    1
2   value2       1       1       0   3.6        Ohio  2002            1    1
3  value11       0       1       0   2.4      Nevada  2001            2    2
4   value5       0       0       0   2.9      Nevada  2002            0    0
5   value9       0       0       1  11.1    New York  2003            3    3
6  value13       0       0       0  23.4    New York  2004            0    0
7  value10       1       1       0   0.1  California  2009            1    1
8   value7       0       0       0   0.3  California  2010            0    0
9  value14       0       1       1   1.1  California  2009            2    2
flag

编辑:

您还可以动态检查包含文字#get columns where first value before _ is text 'flag' cols = [x for x in df.columns if x.split('_')[0] == 'flag'] print cols ['flag_1', 'flag_2', 'flag_3'] #get min value of columns 'flag_1','flag_2','flag_3' print df[cols].idxmax(axis=1) 0 flag_1 1 flag_1 2 flag_1 3 flag_2 4 flag_1 5 flag_3 6 flag_1 7 flag_1 8 flag_1 9 flag_2 dtype: object #get positions of flag print df.columns.get_loc('flag_1') 1 #get positions all flags flag = [df.columns.get_loc(k) for k in df[cols].idxmax(axis=1)] print flag [1, 1, 1, 2, 1, 3, 1, 1, 1, 2] #alternative solution for positions of flags - last digit has to be number print [int(x[-1]) for x in df[cols].idxmax(axis=1)] [1, 1, 1, 2, 1, 3, 1, 1, 1, 2] 的列:

#if all values in 'flag_1','flag_2','flag_3' are 0, get 0 else flag
df['new'] = np.where((df[cols].sum(axis=1)) == 0, 0, flag)
print df
       cat  flag_1  flag_2  flag_3   pop       state  year  new
0   value1       1       0       0   1.5        Ohio  2000    1
1   value3       1       1       0   1.7        Ohio  2001    1
2   value2       1       1       0   3.6        Ohio  2002    1
3  value11       0       1       0   2.4      Nevada  2001    2
4   value5       0       0       0   2.9      Nevada  2002    0
5   value9       0       0       1  11.1    New York  2003    3
6  value13       0       0       0  23.4    New York  2004    0
7  value10       1       1       0   0.1  California  2009    1
8   value7       0       0       0   0.3  California  2010    0
9  value14       0       1       1   1.1  California  2009    2
{{1}}