Reputation: 551
Given a dataframe
Patient ID Instructions ID Replaced
1 N/A ID123
2 ID124
3 ID125
4 xyz ID126
5 xyz ID127
6 ID128
7 Replacement ID129
8 Replace ID130
9 replaced ID131
10 xyz ID132
How can I make a new column, which concats Instructions
with ID Replaced
if replac
substring is found?
Patient ID Instructions ID Replaced Comments
1 N/A ID123
2 ID124
3 ID125
4 xyz ID126
5 xyz ID127
6 ID128
7 Replacement ID129 Replacement | ID129
8 Replace ID130 Replace | ID130
9 Replaced ID131 Replaced | ID131
10 xyz ID132
I've tried the following but the Comments
column is completely empty
mani_df['Comments'] = ""
# if instructions contains 'replac' , concat with ID replaced
if "replace" in df['Instructions']:
df['Comments'] = df['Instructions'].str.cat(df['ID Replaced'], sep = " | ")
and I tried to use a boolean mask but this returns False for the first two rows
mask = mani_df['Special Handling Directions'].str.contains('replac')
Out[55]:
0 False
1 False
2 NaN
3 NaN
Upvotes: 0
Views: 3505
Reputation: 59304
You may usestr.contains
with case=False
and just concat using pandas indexing
mask = df.Instructions.str.contains('Replace', case=False).fillna(False)
df['Comments'] = df.loc[mask, 'Instructions'] + ' | ' + df['ID Replaced']
Of course, you can fillna
at the end to get empty strings (which looks like your expected output)
df.fillna('')
Yields
Patient ID Instructions ID Replaced Comments
0 1 ID123
1 2 ID124
2 3 ID125
3 4 xyz ID126
4 5 xyz ID127
5 6 ID128
6 7 Replacement ID129 Replacement | ID129
7 8 Replace ID130 Replace | ID130
8 9 replaced ID131 replaced | ID131
9 10 xyz ID132
Upvotes: 2