亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據來自不同數據框的列中的條目對數據框應用 Pandas 過濾器(無連接)

如何根據來自不同數據框的列中的條目對數據框應用 Pandas 過濾器(無連接)

守著一只汪 2022-10-25 15:20:04
例如,我有一個數據框 (df_1),其中一列包含一些文本數據。第二個數據框 (df_2) 包含一些數字。如何檢查文本是否包含第二個數據框中的數字?df_1                       Note0  The code to this is 10031  The code to this is 1004df_2   Code_Number0         10061         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    
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

試試這個,看看它是否涵蓋了您的用例:使用itertools 的產品和基于條件的過濾器獲取兩列的交叉笛卡爾:

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


查看完整回答
反對 回復 2022-10-25
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

做這個:

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



查看完整回答
反對 回復 2022-10-25
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

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)) 


查看完整回答
反對 回復 2022-10-25
  • 3 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號