[英]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 ![]() |
1 ![]() |
1000 ![]() |
0.05 ![]() |
chr2 ![]() |
4500 ![]() |
5780 ![]() |
0.0099 ![]() |
chr2 ![]() |
5700 ![]() |
6540 ![]() |
1.3 ![]() |
df1 looks like this: df1 看起来像这样:
Chr![]() |
Start-500![]() |
Start+500![]() |
End-500 ![]() |
End+500![]() |
---|---|---|---|---|
chr1 ![]() |
-499 ![]() |
501 ![]() |
500 ![]() |
1500 ![]() |
chr2 ![]() |
4000 ![]() |
5000 ![]() |
5280 ![]() |
5280 ![]() |
chr2 ![]() |
5200 ![]() |
6200 ![]() |
6040 ![]() |
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.