Python中文
首页
教程
问答
标签
搜索
登录
注册
使用Pandas从另一个数据帧中的信息过滤一个数据帧
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>下面是一个数据帧。在</p> <pre><code>df = pd.DataFrame(columns=['Chromosome', 'Start','End'], data=[ ['chr1', 2000, 3000], ['chr1', 500, 1500], ['chr3', 3000, 4000], ['chr5', 4000, 5000], ['chr17', 9000, 10000], ['chr19', 1500, 2500] ]) </code></pre> <p>我有一个探测数据帧如下。在</p> ^{pr2}$ <p>我想过滤df中包含探针染色体且探针位置介于起始编号和结束编号之间的行,然后将探针名称添加到df中的新列/字段中。所需输出如下:</p> <pre><code> Probe Chrom Start End 0 CG999 chr1 2000 3000 5 CG000 chr19 1500 2500 </code></pre> <p>我下面的尝试是可行的,但是没有将探测名称放入探测列中,并且依赖于循环探测数据。必须有一种更有效的方法来做到这一点。在</p> <pre><code>all_indexes = [] # fake2.tsv is the aforementioned probes dataframe with open('fake2.tsv') as f: for x in f: probe, chrom, pos = x.rstrip("\n").split("\t") row = df[(df['Chromosome'] == chrom) & ((int(pos) > df['Start']) & (int(pos) < df['End']))] all_indexes.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(t.index.tolist()) all_t = [y for x in all_t for y in x] df.iloc[all_indexes] </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你可以试试这个:</p> <pre><code>df.merge(probes, left_on='Chromosome', right_on='Chrom').query('Start < Position < End') </code></pre> <p>输出:</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python闭包能否存储和更改外部标量?
3 回答
python闭包行为怪异
10 回答
python闭包问题源代码编译器
7 回答
Python问gam
3 回答
Python问答游戏逻辑E
4 回答
Python问答程序。你能帮我找出哪里出了问题吗?谢谢
8 回答
Python问答程序问题与分数
10 回答
Python问题
6 回答
Python问题 | 在https://www.testdome.com开发的代码测试分数有bug吗
2 回答
Python问题,上载文件的名称和缩略图
9 回答
python问题,将img的相对url连接到绝对url
9 回答
Python问题,机械化机器人
2 回答
Python问题,用不同的模板发送大量电子邮件?
4 回答
Python问题,类型错误:“float对象不能解释为integer”。无法在“for”循环中返回函数的值
3 回答
Python问题:“overflowerr:Python int太大,无法转换为C long”
7 回答
Python问题:“空数据帧的绑定方法NDFrame.head”
2 回答
Python问题:AttributeError:'_io.TextIOWrapper'对象没有属性'split'
2 回答
Python问题:AttributeError:'dict'对象没有'upper'属性
8 回答
Python问题:CS圆7C循环,正方形普查
10 回答
Python问题:ODE系统的参数估计,例如系数是函数
1 回答