Cannon
Cannon

Reputation: 319

python renaming strings in a column to a specified string

I have a column with many different strings in it and what I want to do is just rename all of the strings that I am specifying to one single string so they all have the same string. So my dataframe looks like this:

          My_strings
1   I bumped my knee because I fell
2   I fell off my bike but I had a helmet
3   I am alright I just need to be alert
4   If I fall I will get back up

So say in my column My_strings I want to look for sentences that contain specific words.

df.loc[df.T_L_DESC.str.contains("fell|fall|fallen", na=False), 'Slippery'] = df.T_L_DESC

The specific words I am looking for are "fell|fall|fallen" once these words are found in the sentences of my column they are then broken into another column called 'Slip_Fall'

I would like to just rename all of the strings that had those words in them to one specific string. One thing to note when I run the above code it makes every sentence that does not contain the words specified in them NaN So my final dataframe would look like this:

            My_strings                           Slippery
1   I bumped my knee because I fell            Life_Lessons   
2   I fell off my bike but I had a helmet      Life_Lessons
3   NaN                                        NaN 
4   If I fall I will get back up               Life_Lessons

So I dont want to obivously change the NaN values I get in my dataframe to Life_Lessons I just want the sentences that contained my keywords to be changed to Life_Lessons

Thanks in advance

Upvotes: 1

Views: 1055

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210872

A straightforward solution:

In [191]: df.loc[df.T_L_DESC.str.contains("fell|fall|fallen", na=False), 'Slippery'] = 'Life_Lessons'

In [192]: df
Out[192]:
                                T_L_DESC      Slippery
0        I bumped my knee because I fell  Life_Lessons
1  I fell off my bike but I had a helmet  Life_Lessons
2   I am alright I just need to be alert           NaN
3           If I fall I will get back up  Life_Lessons

In [193]: df.loc[df.Slippery!='Life_Lessons', 'T_L_DESC'] = np.nan

In [194]: df
Out[194]:
                                T_L_DESC      Slippery
0        I bumped my knee because I fell  Life_Lessons
1  I fell off my bike but I had a helmet  Life_Lessons
2                                    NaN           NaN
3           If I fall I will get back up  Life_Lessons

Upvotes: 2

Related Questions