简体   繁体   English

从一列中删除等于另一列中的值的值

[英]Remove values from one column, that are equal to the value in another

I currently have two columns: 我目前有两列:

Word          Sentence
apple         [this, fruit, is, an, apple]
orange        [orange, is, this, fruit]
grape         [this, is, grape]
strawberry    [strawberry, is, nice]

How would I go about removing the value that appears in df['Word'] from df['Sentence'] so that the output would be: 我将如何从df ['Sentence']中删除df ['Word']中出现的值,以便输出为:

Word          Sentence
apple         [this, fruit, is, an]
orange        [is, this, fruit]
grape         [this, is]
strawberry    [is, nice]

I am currently trying to use this while loop, which is not very pythonic. 我目前正在尝试使用while循环,这不是pythonic。

count_row = df.shape[0]

i=0

while i < count_row :

    mylist = df.iloc[i]["Sentence"]

    mykeyword = df.iloc[i]["Word"]

    mylist = mylist.split()


    for word in mylist:

        if word == mykeyword:

            df.iloc[i]["Sentence"] = df.iloc[i]["Sentence"].replace(word, '')

    print(i)
    i=i+1

However, the loop is not removing the values. 但是,循环不会删除这些值。 What is the best way to achieve the desired output? 实现所需输出的最佳方法是什么?

How about something like... 怎么样...

def remove_name(r): 
    r['Sentence'] = [w for w in r['Sentence'] if w != r['Word']]
    return r

df.apply(remove_name,axis=1)

Apply lets us perform operations like this all at once, no iterations required. Apply使我们可以一次执行所有此类操作,而无需迭代。

You can use remove function to remove an element from a list. 您可以使用删除功能从列表中删除元素。

Syntax: list.remove(element) 语法:list.remove(元素)

Where 'list' is your sentence list and 'element' is your fruit name to be removed. 其中“列表”是您的句子列表,“元素”是您要删除的水果名称。

To know more about remove function refer python docs or this link: https://www.programiz.com/python-programming/methods/list/remove 要了解有关删除功能的更多信息,请参阅python文档或以下链接: https : //www.programiz.com/python-programming/methods/list/remove

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当另一列值等于列表中的值之一时更改列的值 - Change a value of a column when it another column value is equal to one of the values from a list 如果值等于某个数字,则将值从一列复制到另一列 - Copy values from one column to another if values equal to a certain number 如果值相等,则将列值从 dataframe 复制到另一个 - Copy column value from a dataframe to another if values are equal 删除一列中具有相同值而另一列中具有不同值的行 - Delete rows with equal values in one column and different values in another column 如何检查一列中的值是否等于另一列数据框中的值 - How to check if value from one column is equal to value in another columns data-frame 如果 pandas 列值等于另一列的值,则更新它们 - Update pandas column values if they are equal to another column's value 如何在大熊猫不相等的值上将数据从一列移动到另一列? - How do i move data from one column to another on values that aren't equal in pandas? Pandas:使一列的值等于另一列的值 - Pandas: Make the value of one column equal to the value of another 通过两个文件中的一列中的值与另一列中的相应值进行汇总 - Aggregating values in one column by their corresponding value in another from two files 相对于文本文件中另一列中的另一个值,逐个读取列中的每个值 - Read each value one by one in a column with respect to another values in another column from a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM