简体   繁体   English

从熊猫数据框中选择符合条件的行

[英]Selecting rows from a pandas dataframe that match a condition

I have two unequal pandas data frames df and df1.我有两个不相等的熊猫数据框 df 和 df1。 df looks like this: df 看起来像这样:

Chr Start开始 End结尾 Freq频率
chr1 chr1 1 1 1000 1000 0.05 0.05
chr2 chr2 4500 4500 5780 5780 0.0099 0.0099
chr2 chr2 5700 5700 6540 6540 1.3 1.3

df1 looks like this: df1 看起来像这样:

Chr Start-500开始-500 Start+500开始+500 End-500 End-500 End+500结束+500
chr1 chr1 -499 -499 501 501 500 500 1500 1500
chr2 chr2 4000 4000 5000 5000 5280 5280 5280 5280
chr2 chr2 5200 5200 6200 6200 6040 6040 7040 7040

What I want to do is if:我想做的是如果:
- df['chr']==df1['chr'] and - df['chr']==df1['chr']
- df['Start] is in between df1['Start-500] and df1['Start+500'] and - df['Start]介于df1['Start-500]df1['Start+500']并且
- df['End'] is between df1['End-500'] and df1["End+500'] - df['End']df1['End-500']df1["End+500']
then add the Freq to df1 .然后将Freq添加到df1

Please if any one can help me that would be great.请如果有人可以帮助我,那就太好了。 Thank you谢谢

Since your "Start-500", "Start+500" is simply based on the "Start" column (and similarly for "End"), you can simply use pandas.merge_asof :由于您的“Start-500”、“Start+500”仅基于“Start”列(对于“End”也类似),您可以简单地使用pandas.merge_asof

df1["Freq"] = pd.merge_asof(df1, df, left_on="Start+500", right_on="Start")["Freq"]

>>> df1
    Chr  Start-500  Start+500  End-500  End+500    Freq
0  chr1       -499        501      500     1500  0.0500
1  chr2       4000       5000     5280     5280  0.0099
2  chr2       5200       6200     6040     7040  1.3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM