天天看点

python的count变量_在Python中设置给定二进制标志的count变量(pandas dataframe)

您可以在子集中使用flag_1、flag_2和flag_3的子集中的{a1},然后使用^{}通过列表理解找到位置。在

但是所有0值的位置不是0,而是flag_1。所以用numpy.where来纠正它。在#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]

^{pr2}$

编辑:

您还可以使用文本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