【问题标题】:Python/Pandas - remove rows based on conditions below in a dataframe (similar to remove duplicates but not the same) [duplicate]Python/Pandas - 根据数据框中的以下条件删除行(类似于删除重复项但不相同)[重复]
【发布时间】:2021-02-16 21:09:00
【问题描述】:

我正在使用 Python Pandas 来处理下面的数据框:

dataframe:

Company_List1    Company_List2
A                B
A                C
B                D
B                A
D                B
E                F

我想删除下面的行,因为我已经有了 A --> B 和 B -->D:

(但是,我不能简单地使用 drop_duplicates 来完成这项工作)

Company_List1    Company_List2
B                A
D                B

预期输出:

Company_List1    Company_List2
A                B
A                C
B                D
E                F

提前感谢您的帮助!

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    df1=pd.DataFrame(np.sort(df.values, axis=1), columns=df.columns)# Sort values row wise
    df.iloc[df1.drop_duplicates(keep='first').index,:]#exttract inex and mask required
    

    【讨论】:

    • 谢谢!感谢您的帮助
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2020-12-23
    相关资源
    最近更新 更多