简体   繁体   English

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

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

How to delete common things (str, int, float) in one column that I also find in another column?如何删除一列中我也在另一列中找到的常见内容(str、int、float)?

Suppose I have in a dataframe :假设我有一个数据帧:

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

I would like to have this result :我想要这个结果:

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

And this applied to every row in a big pandas Df.这适用于大熊猫 Df 中的每一行。

I did lower the text and tokenized the sentences already but after that I am blocked for the application...我确实降低了文本并已经对句子进行了标记化,但在那之后我被应用程序阻止了......

Thank you谢谢

Try this尝试这个

code to make a 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

code to get expected output:获得预期输出的代码:

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.

相关问题 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 如何检查一个pandas列中列表中的所有元素是否存在于另一个pandas列中 - How to check if all the elements in list in one pandas column are present in another pandas column 如何检查 pandas 列中的字符串列表的元素是否存在于另一列中 - How to check if elements of a list of strings in a pandas column are present in another column 如果存在于另一列的字符串中,则从一列中删除字符串 pandas - Remove string from one column if present in string of another column pandas 如何检查 Pandas 的另一列中是否存在一列中的数据? - How do you check if data in one column is present in another column in Pandas? 熊猫-如何在列中查找一组值,如果存在则在另一列中返回值 - Pandas - How do I look for a set of values in a column and if it is present return a value in another column Python 3 Pandas:如何找到一列的行元素子集与另一列的元素之间的关系? - Python 3 Pandas: How can I find the relationship between a subset of row elements for one column and elements of another column? Pandas 测试一列是否是另一列的子集? (即 B 列包含 A 列) - Pandas test if one column is a subset of another column? (i.e. column B contains column A) 如何使用 pandas 根据第三列中的值创建一个包含来自一列或另一列的值的新列? - How do I create a new column with values from one column or another based on the value in a third column using pandas? 如何使用一列或另一列对 Pandas DataFrame 进行分组 - How do I group a pandas DataFrame using one column or another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM