簡體   English   中英

如何刪除 Pandas 中另一列 B 中存在的 A 列中的常見元素?

[英]How do I delete common elements from one column A that are present in another column B in Pandas?

如何刪除一列中我也在另一列中找到的常見內容(str、int、float)?

假設我有一個數據幀:

colA                              colBB            
eat a nice icecream               icecream            
I love to walk a lot              walk , to          
the city Paris is super           Paris, super  
        .
        .
        .    

我想要這個結果:

colA                    colBB          
eat a nice              icecream          
I love a lot            walk , to           
the city is             Paris, super 
        .
        .
        .      

這適用於大熊貓 Df 中的每一行。

我確實降低了文本並已經對句子進行了標記化,但在那之后我被應用程序阻止了......

謝謝

嘗試這個

制作df的代碼:

df = pd.DataFrame({
    'colA': ['eat a nice icecream', 'I love to walk a lot','the city Paris is super'], 
    'colB': ['icecream', 'walk , to', 'Paris, super']})
    colA                      colB
0   eat a nice icecream       icecream
1   I love to walk a lot      walk , to
2   the city Paris is super   Paris, super

獲得預期輸出的代碼:

df.apply(lambda x: ' '.join([y.strip() for y in x[0].split(' ') if y.strip() not in x[1].split(' ')]), axis=1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM