Fluxy
Fluxy

Reputation: 2978

How to create a new column based on condition?

I have the following data frame df:

col1  col2
1     C
1     B
1     A
2     C
2     C
3     A
3     C
3     B

I need to create a new column col3 and assign T or F per each unique col1: for each unique col1, if at least one row is equal to A in col2, then col3 is equal to T. Otherwise, it's equal to F.

Expected result:

col1  col2  col3
1     C     T
1     B     T
1     A     T
2     C     F
2     C     F
3     A     T
3     C     T
3     B     T

How can I do so? I tried to use apply(lambda ...) solution, but it goes rowwise and assigns T only when col1 is 1 (basically because the last row for 1 is equal to A).

Upvotes: 1

Views: 87

Answers (3)

Karn Kumar
Karn Kumar

Reputation: 8816

Another Solution you can choose with converting int to str with astype('str') with str.contains:

>>> df.assign(col3=df['col1'].astype(str).str.contains('1|3').map({True:'T', False:'F'}))
   col1 col2 col3
0     1    C    T
1     1    B    T
2     1    A    T
3     2    C    F
4     2    C    F
5     3    A    T
6     3    C    T
7     3    B    T

Upvotes: 1

Jaroslav Bezděk
Jaroslav Bezděk

Reputation: 7625

You can also use numpy's where function like this:

>> import numpy as np
>> df['col3'] = np.where(df['col1'].isin(df[df['col2']=='A']['col1'].unique()), 'T', 'F')
>> print(df)
   col1 col2 col3
0     1    C    T
1     1    B    T
2     1    A    T
3     2    C    F
4     2    C    F
5     3    A    T
6     3    C    T
7     3    B    T

Upvotes: 2

BENY
BENY

Reputation: 323226

Check groupby with transform

df['col2'].eq('A').groupby(df['col1']).transform('any')
0     True
1     True
2     True
3    False
4    False
5     True
6     True
7     True
Name: col2, dtype: bool

df['col3']=df['col2'].eq('A').groupby(df['col1']).transform('any').map({True:'T', False:'F'})

Upvotes: 2

Related Questions