Bice
Bice

Reputation: 41

Split Column values based on condition

How do I split the values of a column based on conditions in Pandas? I want to get the column B to be transformed to the first split before the point(.) if the value in A is smaller than 5 For example, I have the following table:

A B
2 ABC.DEF
2 ABC.DEF
8 ABC.DEF
8 ABC.DEF

It should change to

A B
2 ABC
2 ABC
8 ABC.DEF
8 ABC.DEF

Upvotes: 2

Views: 281

Answers (3)

PieCot
PieCot

Reputation: 3629

You can use where, that replaces original values with the ones provided as second argument where the condition (provided as first argument) is False:

df.B.where(df.A.ge(5), df.B.str.split('.').str[0], inplace=True)

df will be turned into:

   A        B
0  2      ABC
1  2      ABC
2  8  ABC.DEF
3  8  ABC.DEF

Upvotes: 1

IoaTzimas
IoaTzimas

Reputation: 10624

Here is one way to do it:

df.loc[df.A<5, 'B']=df.loc[df.A<5, 'B'].apply(lambda x: x.split('.')[0])

print(df)

Output:

   A        B
0  2      ABC
1  2      ABC
2  8  ABC.DEF
3  8  ABC.DEF

Upvotes: 0

Anurag Dabas
Anurag Dabas

Reputation: 24304

Try:

m=df['A'].lt(5)
#your condition

Finally:

df.loc[m,'B']=df.loc[m,'B'].str.split('.').str[0]

OR

#import numpy as np
df['B']=np.where(m,df['B'].str.split('.').str[0],df['B'])

Output of df:

    A   B
0   2   ABC
1   2   ABC
2   8   ABC.DEF
3   8   ABC.DEF

Upvotes: 1

Related Questions