【问题标题】:How to check df1 rows with all single rows of another df2 columns to check if same in dataframe如何检查 df1 行与另一个 df2 列的所有单行以检查数据框中是否相同
【发布时间】:2021-09-17 23:30:52
【问题描述】:

我见过很多这样的代码:

mergedStuff = pd.merge(df1, df2, how='inner')

or

mask = df1.reindex(df2.index).values == df2.values

但以上仅在同一行存在于其相对行中时才给出它不与每一行进行比较

例如:

 df1 contains:
  
  0

  hello  
  how
  are
  you 
  guys
  system

  df2 contains:
  
  0             1   ........ n

  how           hello        you
  hello         guys         hello
  you           system       how 
  are           you          you
  guys          hello        hello
  system        how          how
  hello         are          system

更新:比较 df2.isin(df1) 后的最终输出列:

  #NOTE ! below output are manually entered, not from real output
  #        but i know this is how it gives.
  false   True   false
  false   false  false
  false   false  false
  false   false  false
  false   false  false
  false   false  false
  false   false  false      #only second column and second row are 
                                      #  true, because 
                                       # it matches 
                                       # the same row

但我想要的是交叉检查每个 df1 行和 df2 行。

预期输出:

  True  True  True
  True  True  True
  True  True  True
  True  True  True
  True  True  True
  True  True  True
  True  True  True    #i want true for all Because every rows has 
                       # the same word.

更新2:

但是,如果我这样运行,那么它会给出预期的输出:

df2[2].isin(df1[0])

 True
 True
 True
 True
 True
 True
 True  # 2nd column of df2 compared with df1 and gives good output.
        # but if i give without index it gives crap.

如果你想帮忙,这里是测试的输入:

df1 = pd.DataFrame({0: 
['hello','how','are','you','guys','system']})

   df2 = pd.DataFrame({ 0: 
  ['how','hello','you','hello','guys','hello',
    'you','system','how','are','you','you' 
     'guys','hello','hello','system','how',
      'how','hello','are','system'],

    1: ['how','you','you','hello','guys',
      'hello','you','system','how','are','you','you' 
       'guys','hello','hello','system',
       'how','hello','hello','are','system'] ,

      2: ['how','you','you','are','guys',
     'hello','you','system','you','are','guys','you' 
          'guys','hello','hello','system',
        'how','hello','hello','are','system']
            })

这个终于成功了:

new = np.isin(df2, df1)
rows, cols = np.nonzero(~new)

#or

new = np.isin(df2, df1, invert=True)
rows, cols = np.nonzero(new)


x2 = []

for item in zip(rows,cols):
    x2.append(df2.iloc[item]) 

【问题讨论】:

  • 先对它们进行排序怎么样?
  • 你到底想要什么?未出现的单词列表?
  • 是的,如果您运行该代码,则会发生错误,我理解错误但不知道如何解决。在获得所有错误值之后,我可以在 excel 中为该值赋予颜色,这就是原因。
  • @Corralien 嗨,如果可能的话,你能检查一下这个帖子并回答吗?我谦虚的请求。 stackoverflow.com/questions/68314626/…

标签: python regex string dataframe sorting


【解决方案1】:

试试pandas.DataFrame.isin

df1 = pd.DataFrame(['hello', 'how', 'are', 'you'], columns=['column1'])

df2 = pd.DataFrame(['hello', 'are', 'you', 'how'], columns=['column1'])

df1["column1"].isin(df2["column1"])

# 0    True
# 1    True
# 2    True
# 3    True
# Name: column1, dtype: bool

注意:恐怕是O(n*m) 复杂性,这意味着对于 df1 的每个元素,您都在迭代 df2,尽管我不知道实际的实现。更快的方法是对两列进行排序并检查是否相等。

df1 = pd.DataFrame(['hello', 'how', 'are', 'you'], columns=['column1'])

df2 = pd.DataFrame(['hello', 'are', 'you', 'how'], columns=['column1'])

df1.sort_values(['column1'], inplace=True, ignore_index=True)
df2.sort_values(['column1'], inplace=True, ignore_index=True)

df1['column1'] == df2['column1']
0    True
1    True
2    True
3    True
Name: column1, dtype: bool

【讨论】:

    【解决方案2】:

    使用np.in1d:

    >>> df2.apply(lambda x: np.in1d(x, df1[0]))
            0      1      2
    0    True   True   True
    1    True   True   True
    2    True   True   True
    3    True   True   True
    4    True   True   True
    5    True   True   True
    6    True   True   True
    7    True   True   True
    8    True   True   True
    9    True   True   True
    10   True   True   True
    11  False  False  False
    12   True   True   True
    13   True   True   True
    14   True   True   True
    15   True   True   True
    16   True   True   True
    17   True   True   True
    18   True   True   True
    19   True   True   True
    

    更新:缺失单词列表

    >>> set(np.extract(mask == False, df2))
    {'youguys'}
    

    【讨论】:

    • 哇,谢谢哥们!工作得很好,但你能再讲一件事吗?我已经在主查询中更新了
    【解决方案3】:
    new = np.isin(df2, df1)
    rows, cols = np.nonzero(~new)
    
    #or
    
    new = np.isin(df2, df1, invert=True)
    rows, cols = np.nonzero(new)
    
    
    x2 = []
    
    for item in zip(rows,cols):
        x2.append(df2.iloc[item])
    

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 2020-09-20
      • 2019-07-06
      • 2019-06-21
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多