【问题标题】:Remove rows from a data frame based on the value of another data frame根据另一个数据框的值从一个数据框中删除行
【发布时间】:2021-09-22 10:17:41
【问题描述】:

我想知道我该怎么做,

我有一个像这样的数据框:

df1

Tweet           Other columns  ....
-------         -----------    -----    
"Hello world"      ...         ...
"Good morning"     ...         ...
"Hi"               ...         ...

还有一个像这样的:

df2

Tweet           Other columns    ....
-------         -----------     
"Yes"              ...           ...
"Test"             ...           ...
"Hello world"      ...           ...

我想从 df2 中删除出现在 df1 的 Tweet 列中的行。

还有这样的东西:

df2

Tweet           Other columns  ....
-------         -----------    -----    
"Yes"               ...         ...
"Test"              ...         ...
"Hi"                ...         ...

(我删除了 Tweet 值 = "Hello world" 的行)因为此文本出现在第一个数据框的 Tweet 列中。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    也许试试np.where:

    df2['Tweet'] = np.where(df2['Tweet'].isin(df1['Tweet']), df1['Tweet'], df2['Tweet'])
    

    或者更短的使用loc:

    df2.loc[df2['Tweet'].isin(df1['Tweet']), 'Tweet'] = df1['Tweet']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2020-11-13
      • 2018-06-22
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多