1

I have a dataframe with 6 columns like that:

c1      c2             c3      c4           c5      c6
C875    DOID_3263       1       9.65E-18    1       unknown
C783    DOID_4064       1       4.80E-17    1       unknown
C372    DOID_0050084    0.996   0.00429     0.996   unknown
C43     DOID_936        0.0457  0.954       0.954   known

Column c5 represent the max value between c3 and c4, I want to add a column after c6 to compare if the max value in c5 come from c3 put 0 if it come from c4 put 1

So, at the end the final result will be like this:

c1      c2              c3      c4          c5      c6       c7
C875    DOID_3263       1       9.65E-18    1       known    0
C783    DOID_4064       1       4.80E-17    1       unknown  0
C372    DOID_0050084    0.996   0.00429     0.996   unknown  0
C43     DOID_936        0.0457  0.954       0.954   known    1

Any help, please?

0

3 Answers 3

1

First, take the max of the two columns

df['c5'] = np.maximum(df['c3'], df['c4'])

If the max is equal to 'c4' put a 1, else 0 (which implies it came from 'c3' under this paradigm).

df['c7'] = (df['c5'] == df['c4']).astype(int)
Sign up to request clarification or add additional context in comments.

Comments

1

Using idxmax yields the name of the columns right away

df[['c3','c4']].idxmax(1)

0    c3
1    c3
2    c3
3    c4
dtype: object

Can always map if need 0 or 1

df[['c3','c4']].idxmax(1).map({'c3': 0, 'c4':1})

0    0
1    0
2    0
3    1
dtype: int64

1 Comment

Nice solution, I had no idea idmax could be used in this fashion.
0

Using select from numpy

s1=df.c3==df.c5
s2=df.c4==df.c5
df['c7']=np.select([s1,s2],[0,1])
df
Out[670]: 
     c1            c2      c3            c4     c5       c6  c7
0  C875     DOID_3263  1.0000  9.650000e-18  1.000  unknown   0
1  C783     DOID_4064  1.0000  4.800000e-17  1.000  unknown   0
2  C372  DOID_0050084  0.9960  4.290000e-03  0.996  unknown   0
3   C43      DOID_936  0.0457  9.540000e-01  0.954    known   1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.