我有一個 Pandas 資料框,如下所示:
email col2 col3
[email protected] John Doe
[email protected] John Doe
[email protected] John Doe
[email protected] John Doe
[email protected] Jane Doe
我想查看每個以至少兩個“x”開頭的電子郵件地址,并檢查是否存在沒有那些“x”的相同電子郵件地址。
要求的結果:
email col2 col3 exists_in_valid_form
[email protected] John Doe False
[email protected] John Doe True
[email protected] John Doe True
[email protected] John Doe True
[email protected] Jane Doe False
我能夠得到一個包含所有那些行的子資料框,其中電子郵件以“xx”開頭,使用df[df['email'].str.contains("xx")],并且還能夠在不使用“x”的情況下獲取電子郵件地址str.lstrip('x'),但兩者似乎都沒有幫助我了解是否這封電子郵件出現在沒有那些 x 的其他地方。
uj5u.com熱心網友回復:
您可以使用duplicated()來獲取值是否存在于其他行中。
df['exists_in_valid_form'] = df.email.str.lstrip('x').duplicated(keep=False) & df.email.str.startswith('xx')
我添加df.email.str.startswith('xx')以確保它應該以至少 2 個“x”開頭,并為“[email protected]”回傳 False。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360310.html
