亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據pandas中另一列的長度獲取子字符串

根據pandas中另一列的長度獲取子字符串

素胚勾勒不出你 2021-09-14 21:18:50
我有一個數據框,       plan_identifier wellthie_issuer_identifier0  UNM99901AL0000001-DEN                   UNM999021  UNM99902AK0000001-DEN                   UNM999022  UNM99904AZ0000001-DEN                   UNM999043  UNM99905AR0000001-DEN                   UNM999054  UNM99906CA0000001-DEN                   UNM999065  UNM99908CO0000001-DEN                   UNM999096  UNM99909CT0000001-DEN                   UNM99909我需要檢查plan_identifier得到長度后考慮的子串wellthie_issuer_identifier是否相等?Ex-length ofUNM99902是 8 ,所以我的plan_identifier子串 = UNM99901?,F在這應該返回給我 False。所以,只要這不相等,我就應該得到 False。我的輸出應該是:-FALSETRUETRUETRUETRUEFALSETRUE我試過類似下面的東西-print(~(df['plan_identifier'].str[:(df['wellthie_issuer_identifier'].astype(str).str.len())] != df['wellthie_issuer_identifier']))如何實現這一目標?我們可以使用apply()嗎?
查看完整描述

2 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

使用defchararray.find來自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])


查看完整回答
反對 回復 2021-09-14
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

Pandas 中的字符串方法通常很慢。您可以改用列表理解。IUC:


>>> [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

[編輯]在評論中,您說您只對字符串的開頭感興趣。在這種情況下,您可以startswith改用:


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


查看完整回答
反對 回復 2021-09-14
  • 2 回答
  • 0 關注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號