Python:在多列中查找字符串,然后在新列中返回 [英] Python: find string in multiple columns and return it in new column

查看:49
本文介绍了Python:在多列中查找字符串,然后在新列中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多列的excel数据,我需要对特定的单词进行罚款并在新列中将其返回桌子看起来像这样:

Hi I have an excel data with multiple columns and i need to fined specific word and return it in new column the table look like this:

ID   col0  col1  col2  col3  col4  col5
1    jack  a/h   t/m   w/n   y/h    56
2    sam   z/n   b/w   null  null   93
3    john  b/i   y/d   p/d   null   33

我想在col1,col2,col3和col4列中查找"b",并创建一个名为"b"的新列,在该列中将返回带有be的单元格值

I want to look for 'b' in columns col1, col2, col3, and col4 and create a new column called "b" where the value the cell value with be is returned

结果看起来像这样

ID   col0  col1  col2  col3  col4  col5  b
1    jack  a/h   t/m   w/n   y/h    56   -
2    sam   z/n   b/w   null  null   93   b/w
3    john  b/i   y/d   p/d   null   33   b/i

我需要一种有效的方法来尝试在这样的地方使用

and I need an efficient way to do it I tried to use where like this

df1 = df[['col1', 'col2', 'col3', 'col4']]

df1['b']==[x for x in df1.values[0] if any(b for b in lst if b in str(x))]

我是从这个答案中得到的 https://stackoverflow.com/a/50250103/3105140

I got this from this answer https://stackoverflow.com/a/50250103/3105140

但是它对我不起作用,因为我的值是空的并且条件不起作用的行

yet it is not working for me snice I have null value and rows where the condition do not work

推荐答案

以下是使用

Here is a way using stack and str.contains with df.where:

cols = ['col1', 'col2', 'col3', 'col4']
df['b'] = (df[cols].where(df[cols].stack().str.contains('b')
         .unstack(fill_value=False)).ffill(1).iloc[:,-1])


print(df)

   ID  col0 col1 col2 col3 col4  col5    b
0   1  jack  a/h  t/m  w/n  y/h    56  NaN
1   2   sam  z/n  b/w  NaN  NaN    93  b/w
2   3  john  b/i  y/d  p/d  NaN    33  b/i

这篇关于Python:在多列中查找字符串,然后在新列中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆