python 将列中的字符串重命名为指定字符串
python renaming strings in a column to a specified string
我有一列包含许多不同的字符串,我想做的只是将我指定的所有字符串重命名为一个字符串,以便它们都具有相同的字符串。所以我的数据框看起来像这样:
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
所以在我的专栏中说 My_strings 我想查找包含特定单词的句子。
df.loc[df.T_L_DESC.str.contains("fell|fall|fallen", na=False), 'Slippery'] = df.T_L_DESC
我要查找的特定词是 "fell|fall|fallen" 一旦在我的专栏的句子中找到这些词,它们就会被分解到另一个名为 'Slip_Fall'
的栏中
我只想将所有包含这些词的字符串重命名为一个特定的字符串。当我 运行 上面的代码时要注意一件事,它使每个句子都不包含其中指定的单词 NaN 所以我的最终数据框看起来像这样:
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
所以我不想明显地将我在数据框中获得的 NaN 值更改为 Life_Lessons 我只想将包含我的关键字的句子更改为 Life_Lessons
提前致谢
一个简单的解决方案:
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
我有一列包含许多不同的字符串,我想做的只是将我指定的所有字符串重命名为一个字符串,以便它们都具有相同的字符串。所以我的数据框看起来像这样:
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
所以在我的专栏中说 My_strings 我想查找包含特定单词的句子。
df.loc[df.T_L_DESC.str.contains("fell|fall|fallen", na=False), 'Slippery'] = df.T_L_DESC
我要查找的特定词是 "fell|fall|fallen" 一旦在我的专栏的句子中找到这些词,它们就会被分解到另一个名为 'Slip_Fall'
的栏中我只想将所有包含这些词的字符串重命名为一个特定的字符串。当我 运行 上面的代码时要注意一件事,它使每个句子都不包含其中指定的单词 NaN 所以我的最终数据框看起来像这样:
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
所以我不想明显地将我在数据框中获得的 NaN 值更改为 Life_Lessons 我只想将包含我的关键字的句子更改为 Life_Lessons
提前致谢
一个简单的解决方案:
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