Reputation: 319
I am wondering if it is even possible to do what I want. I am right now using
df.loc[df.T_LOSS_DESC.str.contains("slip", na=False)]
It locates the column T_LOSS_DESC and then anywhere in that column where their is the specific word like "slip" it returns those rows. My first question is there anyway to put the results in its own column? also if so is their anyway to specify more than one possible keyword to look for? Example would be
df.loc[df.T_LOSS_DESC.str.contains("slip,Slip,Slipped", na=False)]
is that viable to do? or can I only use one parameter?
what my dataframe looks like:
T_LOSS_DESC
1 Bob was running and Slipped
2 Jeff got burnt by the sun
3 James went for a walk
what I would like my dataframe to look like is if it finds matches inside that column I am looking at I want it to put the matches in a different column.
So my final dataframe would look like such:
T_LOSS_DESC Slippery
1 Bob was running and Slipped Bob was running and Slipped
2 Jeff got burnt by the sun
3 James went for a walk
so since only one of my strings matched with the strings i was looking for in the column it would then bring that one match over into a new column called Slippery
Thanks in advance.
Upvotes: 1
Views: 53
Reputation: 210852
IIUC:
In [95]: df['new'] = df.loc[df.T_LOSS_DESC.str.contains("slip|Slip|Slipped", na=False)]
In [96]: df
Out[96]:
T_LOSS_DESC new
0 Bob was running and Slipped Bob was running and Slipped
1 Jeff got burnt by the sun NaN
2 James went for a walk NaN
alternatively you can do it this way:
In [116]: df.loc[df.T_LOSS_DESC.str.contains("slip|Slip|Slipped", na=False), 'Slippery'] = df.T_LOSS_DESC
In [117]: df
Out[117]:
T_LOSS_DESC Slippery
0 Bob was running and Slipped Bob was running and Slipped
1 Jeff got burnt by the sun NaN
2 James went for a walk NaN
Upvotes: 2