I have some pandas dataframe:
a,b,c
AAA,,
DDD,,
KKK,,
AAA,,
I want to search in column "A" and if string in column "A" contains word "AAA" I need set value "BBB" in column "B" and "CCC" in column "C".
So, I want to get result as following:
a,b,c
AAA,BBB,CCC
DDD,,
KKK,,
AAA,BBB,CCC
I wrote the code with numpy:
df['b'] = pd.np.where(df.a.str.contains("AAA"), "BBB", '')
How to extend it to work with 'b' and 'c' columns?
You can use double np.where
:
mask = df.a.str.contains("AAA")
df['b'] = pd.np.where(mask, "BBB", '')
df['c'] = pd.np.where(mask, "CCC", '')
Or assign
:
mask = df.a.str.contains("AAA")
df = df.assign(b=pd.np.where(mask, "BBB", ''), c=pd.np.where(mask, "CCC", ''))
If need create multiple columns with one np.where
is necessary create Nx1
mask:
mask = df.a.str.contains("AAA")[:, None]
df[['b','c']] = np.where(mask, ['BBB','CCC'], ['',''])
print (df)
a b c
0 AAA BBB CCC
1 DDD
2 KKK
3 AAA BBB CCC
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments