Python 基于表中另一列的长度获取子字符串

Python 基于表中另一列的长度获取子字符串,python,pandas,Python,Pandas,我有一个数据框 plan_identifier wellthie_issuer_identifier 0 UNM99901AL0000001-DEN UNM99902 1 UNM99902AK0000001-DEN UNM99902 2 UNM99904AZ0000001-DEN UNM99904 3 UNM99905AR0000001-DEN

我有一个数据框

       plan_identifier wellthie_issuer_identifier
0  UNM99901AL0000001-DEN                   UNM99902
1  UNM99902AK0000001-DEN                   UNM99902
2  UNM99904AZ0000001-DEN                   UNM99904
3  UNM99905AR0000001-DEN                   UNM99905
4  UNM99906CA0000001-DEN                   UNM99906
5  UNM99908CO0000001-DEN                   UNM99909
6  UNM99909CT0000001-DEN                   UNM99909
我需要检查在获得
wellthie\u issuer\u identifier
的长度后考虑的
plan\u identifier
的子字符串是否相等

Ex-UNM99902的长度是8,所以我的
计划标识符
子字符串=
UNM99901
。现在这应该是我的错误

所以,如果这不相等,我就错了

我的输出应该是:-

FALSE
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
我试过下面的方法-

print(~(df['plan_identifier'].str[:(df['wellthie_issuer_identifier'].astype(str).str.len())] != df['wellthie_issuer_identifier']))

如何做到这一点?我们可以使用apply()吗?

熊猫中的字符串方法通常非常慢。您可以改为使用列表理解。IIUC:

>>> [i in p for p,i in zip(df['plan_identifier'],df['wellthie_issuer_identifier'])]
[False, True, True, True, True, False, True]

# or assign to new column:

df['new_column'] = [i in p for p,i in zip(df['plan_identifier'],df['wellthie_issuer_identifier'])]
>>> df
         plan_identifier wellthie_issuer_identifier  new_column
0  UNM99901AL0000001-DEN                   UNM99902       False
1  UNM99902AK0000001-DEN                   UNM99902        True
2  UNM99904AZ0000001-DEN                   UNM99904        True
3  UNM99905AR0000001-DEN                   UNM99905        True
4  UNM99906CA0000001-DEN                   UNM99906        True
5  UNM99908CO0000001-DEN                   UNM99909       False
6  UNM99909CT0000001-DEN                   UNM99909        True
[EDIT]在评论中,您说您只对字符串的开头感兴趣。在这种情况下,您可以使用
startswith

[p.startswith(i) for p,i in zip(df['plan_identifier'],df['wellthie_issuer_identifier'])]

使用
defchararray.find
from
numpy

s1=df.plan_identifier.values.astype(str)
s2=df.wellthie_issuer_identifier.values.astype(str)    
~np.core.defchararray.find(s1,s2).astype(bool)
 Out[64]: array([False,  True,  True,  True,  True, False,  True])

标识符的位置重要吗,还是只想看看它是否在那里?是的。。我需要检查同一行中的两个列valuesSorry,我指的是字符串中的位置。标识符必须在开头吗?是的。它将在开始时对行进行验证。我需要输出为dataframe bool类型。我正在对行进行验证。我需要作为数据帧bool类型的输出。