比较列中的字符串并在python中创建相应的新列 [英] comparing string in a column and creating respective a new column in python
本文介绍了比较列中的字符串并在python中创建相应的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所示的数据帧.我需要将数据框中的列与字符串进行比较,然后创建一个新列.
I have data frame as shown below. I need to compare column in a data frame with the string and creating a new column.
DataFrame:
DataFrame:
col_1
AB_SUMI
AK_SUMI
SB_LIMA
SB_SUMI
XY_SUMI
如果col_1中存在"AB","AK","SB",则应使用各自的值创建一个新列,否则应在列值中添加"*".
If 'AB','AK','SB' are present in col_1 it should create a new column with their respective values otherwise '*' should come in the column value.
预期输出:
col_1 new_col
AB_SUMI AB
AK_SUMI AK
SB_LIMA SB
SB_SUMI SB
XY_SUMI *
我尝试使用下面的代码,但是没有解决.
I have tried with below code but not worked out.
list=['AB','AK','AB']
for item in list:
if df['col1'].str.contains(item).any():
df['new']=item
在这方面请帮助我.预先感谢
please help me in this regard. Thanks in advance
推荐答案
You can use extract
with regex
created with list
by join
|
(or
), last replace NaN
by fillna
:
L= ['AB','AK','SB']
a = '(' + '|'.join(L) + ')'
print (a)
(AB|AK|SB)
df['new'] = df.col_1.str.extract(a, expand=False).fillna('*')
print (df)
col_1 new
0 AB_SUMI AB
1 AK_SUMI AK
2 SB_LIMA SB
3 SB_SUMI SB
4 XY_SUMI *
这篇关于比较列中的字符串并在python中创建相应的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文