【问题标题】:How to apply a Pandas filter on a data frame based on entries in a column from a different data frame (no join)如何根据来自不同数据框的列中的条目对数据框应用 Pandas 过滤器(无连接)
【发布时间】:2020-04-27 12:38:15
【问题描述】:

例如,我有一个数据框 (df_1),其中一列包含一些文本数据。第二个数据框 (df_2) 包含一些数字。如何检查文本是否包含第二个数据框中的数字?

df_1

                       Note
0  The code to this is 1003
1  The code to this is 1004

df_2

   Code_Number
0         1006
1         1003

所以我想检查 df_1 中 [Note] 中的条目是否包含 df_2 中 [Code_Number] 中的条目

我已尝试使用以下代码:df_1[df_1['Note'].str.contains(df_2['Code_Number'])],并且我知道我无法使用连接,因为我没有用于连接的密钥。

应用过滤后我正在寻找的最终结果是:

   Note              
0  The code to this is 1003    

【问题讨论】:

    标签: python pandas data-analysis


    【解决方案1】:

    试试这个,看看它是否涵盖了您的用例:使用itertools' product 获取两列的交叉笛卡尔并根据条件进行过滤:

    from itertools import product
    m = [ left for left, right
          in product(df.Note,df1.Code_Number) 
          if str(right) in left]
    
    pd.DataFrame(m,columns=['Note'])
    
                   Note
    0   The code to this is 1003
    

    【讨论】:

      【解决方案2】:

      这样做:

      df_1.loc[df_1['Note'].apply(lambda x: any(str(number) in x for number in df_2['Code_Number']))]
      

      【讨论】:

      • 所以我得到了:df_i = df_1.loc[df_1['Note'].apply(lambda x: any(str(number) in x for number in df_2['Code_Number'])) return df_i 但是当我运行它时,它说'无效语法'
      • 抱歉有错字,@sammywemmy 已修复。现在应该运行。
      【解决方案3】:
      Firstly, you have to create 1 column in your df1 where the notes are with a list of numbers that are present in the Notes and then Compare the List column of numbers with the List column of the df2 where the numbers are present(both should be in list format)
      
      
      
      #Extract Numbers from Notes
      a_string = "0abcadda1 11 def 23 10007"
      
      numbers = [int(word) for word in a_string.split() if word.isdigit()]
      
      print(numbers)
      
      
      list_test = "103,23"
      
      #Finding common element from both lists the list
      L1 = [2,3,4]
      L2 = [1,2]
      [i for i in L1 if i in L2]
      
      
      S1 = set(L1)
      S2 = set(L2)
      print(S1.intersection(S2))
      
      #If you want to find out the common element
      
      def common_data(list1, list2):
          result = False
      
          # traverse in the 1st list 
          for x in list1:
      
              # traverse in the 2nd list 
              for y in list2:
      
                  # if one common 
                  if x == y:
                      result = True
                      return result
      
          return result
      
      
      # driver code 
      
      a = [1, 2, 3, 4, 5]
      b = [5, 6, 7, 8, 9]
      print(common_data(a, b))
      
      a = [1, 2, 3, 4, 5]
      b = [6, 7, 8, 9]
      print(common_data(a, b)) 
      

      【讨论】:

        猜你喜欢
        • 2017-11-03
        • 1970-01-01
        • 2023-02-22
        • 2019-03-29
        • 2023-03-20
        • 2020-08-29
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        相关资源
        最近更新 更多