将指定的字符串分配到数据框

时间:2017-04-27 18:29:09

标签: python pandas dataframe

我想知道是否有可能做我想做的事。我现在正在使用

df.loc[df.T_LOSS_DESC.str.contains("slip", na=False)]

它找到列T_LOSS_DESC,然后在该列中的任何位置,它们是特定单词,如“slip”,它返回那些行。我的第一个问题是将结果放在自己的专栏中?如果是这样,他们还是要指定多个可能的关键字来寻找?例子是

 df.loc[df.T_LOSS_DESC.str.contains("slip,Slip,Slipped", na=False)]

可行吗?或者我只能使用一个参数?

我的数据框架是什么样的:

         T_LOSS_DESC 
1 Bob was running and Slipped
2 Jeff got burnt by the sun
3 James went for a walk

我希望我的数据框看起来如果它在该列中找到匹配我正在查看我希望它将匹配放在不同的列中。

所以我的最终数据框看起来像这样:

         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

因为我的字符串只有一个与我在列中寻找的字符串相匹配,所以它会将一个匹配带到一个名为Slippery的新列中

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

或者你也可以这样做:

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