簡體   English   中英

根據與熊貓的部分匹配來合並列

[英]Merge columns based on partial match with pandas

我有2個DFS,我想通過以下方式合並它們:
-在X列上完全匹配。
-數字在YZpdf應在那些范圍odf ,即使只是部分。

#odf
      X     Y    Z 
b1    s1    3    19
b2    s1    5    300
b4    s3    500  550
b6    s5    5    25

#pdf
      X     Y    Z
d3    s2    7    12   #wrong s
d6    s1    50   220  #match b2 above 
d7    s3    503  509  #match b4 above
d16   s5    15   30   #accept match to b6, partial match in Y/Z.
d18   s5    4    15   #accept match to b6  

在這種情況下,我會得到:

#iodf and ipdf are indices of the two dfs above
iodf    X     Yodf    Zodf   ipdf    Ypdf   Zpdf
b2      s1    5       300    d6      50     220   
b4      s3    500     550    d7      503    509
b6      s5    5       25     d16     15     30 
b6      s5    5       25     d18     4      15

我正在考慮在每個df中創建一個帶有正則表達式的附加列,並根據該正則表達式合並它們。

odf.loc[:,'id']=odf.X+'\\_`+odf.Y.astype(str)+'\\_`+odf.Z.astype(str)
pdf.loc[:,'id']=pdf.X+'\\_`+pdf.Y.astype(str)+'\\_`+pdf.Z.astype(str)

問題在於,然后我需要將YZ的值指定為范圍,但是我不確定如何解決這一點。 有什么建議么? 在此先多謝!

IIUC,您可以執行以下操作:

df = odf.reset_index().merge(pdf.reset_index(), on='X', suffixes=('odf','pdf'))

cleaned = df[(df['Ypdf'].between(df['Yodf'], df['Zodf'])) | (df['Zpdf'].between(df['Yodf'], df['Zodf']))]

產量:

  indexodf   X  Yodf  Zodf indexpdf  Ypdf  Zpdf
1       b2  s1     5   300       d6    50   220
2       b4  s3   500   550       d7   503   509
3       b6  s5     5    25      d16    15    30
4       b6  s5     5    25      d18     4    15

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM