简体   繁体   English

熊猫:删除其中一列的值出现在另一列中的任何行的行

[英]Pandas: Remove Rows Where the Value of One Column Appears on Any Row in Another

Example data is: 示例数据是:

000000008,2, 1,000000010
000000009,1, 1,000000011
000000010,1, 1,000000008
000000011,2, 1,000000032
000000012,3, 1,000000009
000000013,2, 1,000000108

You can see that some values in the first column also appear in the fourth column. 您可以看到第一列中的某些值也出现在第四列中。 I want to remove those rows, where the value in the fourth column also appears on any row in the first column. 我要删除那些行,其中第四列中的值也会出现在第一列中的任何行上。

Therefore, in this example, following rows should be removed: 因此,在此示例中,应删除以下行:

000000008,2, 1,000000010
000000010,1, 1,000000008
000000012,3, 1,000000009
000000009,1, 1,000000011

Code starting point: 代码起点:

import numpy as np
import pandas as pd

T = u'''000000008,2, 1,000000010
    000000009,1, 1,000000011
    000000010,1, 1,000000008
    000000011,2, 1,000000032
    000000012,3, 1,000000009
    000000013,2, 1,000000108'''

from io import StringIO
df = pd.read_csv(StringIO(T), header=None)
print(df)

IIUC, from your description, you can do: IIUC,根据您的描述,您可以执行以下操作:

df[~df.iloc[:,3].isin(df.iloc[:,0])]

Which returns: 哪个返回:

    0  1  2    3
3  11  2  1   32
5  13  2  1  108

Contrary to your desired output, this removes the row with 000000011 , but not the one with 000000108 , because 000000011 is found in both columns, but 000000108 is not 相反,你需要的输出,这消除了一行000000011 ,而不是一个与000000108 ,因为000000011两列中发现,但000000108

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

相关问题 在 Pandas 数据框中删除一个值以另一行的值开头的行的更多 Pythonic 方法 - More pythonic way to remove rows where one value begins by another row's value in a pandas dataframe 删除一列中的值等于另一列中的值的行 - Remove rows where value in one column equals value in another 从Pandas数据框中删除行,其中值仅出现一次 - Remove rows from Pandas dataframe where value only appears once 熊猫当列中出现某种特定类型的值时,删除一行 - Pandas Remove a row when a particular kind of value appears in a column 如何根据列值删除行,其中某行的列值是另一行的子集? - How to remove rows based on a column value where some row's column value are subset of another? 在 Pandas 中选择行,其中一列中的值是另一列中值的子字符串 - Select rows in pandas where value in one column is a substring of value in another column Python Pandas Dataframe,删除“无”是任何列中的值的所有行 - Python Pandas Dataframe, remove all rows where 'None' is the value in any column Pandas:删除任何列包含某个子字符串的所有行 - Pandas: Remove all rows where any of the column contains a certain substring 删除在 Pandas 中另一行的值为 substring 的行 - Remove row where value is found as a substring of another row in Pandas Pandas:如果关键字出现在任何列中,请选择行 - Pandas: select rows if keyword appears in any column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM