python - 位于指定范围内的值的数量

标签 python unix pandas awk

我有一个如下所示的数据框:

NC_011163.1:1
NC_011163.1:22
NC_011163.1:44
NC_011163.1:65
NC_011163.1:73
NC_011163.1:87
NC_011163.1:104
NC_011163.1:130
NC_011163.1:151
NC_011163.1:172
NC_011163.1:194
NC_011163.1:210
NC_011163.1:235
NC_011163.1:255
NC_011163.1:295
NC_011163.1:320
NC_011163.1:445
NC_011163.1:520

我想使用 210 窗口扫描数据框并计算每个 210 窗口中的值数量。

期望的输出:

output: Values 
NC_011163.1:1-210   12
NC_011163.1:211-420 4
NC_011163.1:421-630 2

非常感谢您为解决此问题提供的意见。

谢谢

V

最佳答案

如果您使用 python 和 Pandas ,你可以这样做:

将数据放在数据框中df:

             NC    N
0   NC_011163.1    1
1   NC_011163.1   22
2   NC_011163.1   44
3   NC_011163.1   65
4   NC_011163.1   73
5   NC_011163.1   87
6   NC_011163.1  104
7   NC_011163.1  130
8   NC_011163.1  151
9   NC_011163.1  172
10  NC_011163.1  194
11  NC_011163.1  210
12  NC_011163.1  235
13  NC_011163.1  255
14  NC_011163.1  295
15  NC_011163.1  320
16  NC_011163.1  445
17  NC_011163.1  520

df.groupby([df.NC, pd.cut(df.N, range(0,631,210))]).count()
                         N
NC          N             
NC_011163.1 (0, 210]    12
            (210, 420]   4
            (420, 630]   2

地点:

  • pd.cut(df.N, range(0, 631, 210)) 返回 N 列中的值所在的 bin。 bin 由范围定义,这会创建 3 个 bin:[0, 210, 420, 630]
  • 然后你分组:
    • NC 编号(因此它与您的输出匹配,但这里没有用,因为只有一组,但我猜您会有其他染色体,因此它将执行每个染色体的操作)
    • 你刚刚制作的垃圾箱
  • count 每组中元素的数量。

关于python - 位于指定范围内的值的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39017766/

相关文章:

linux - 如何grep美元符号($)?

python - Pandas 系列到 Pandas Dataframe

python - keras 中用于评估符号预测的自定义指标

PostgreSQL fe_sendauth : no password supplied

Python 数据帧 : Merging two dataframes according to a condition (Pandas)

linux - 远程 SSH 的 Shell 脚本

pandas 将 float64 转换为 int

python - 有列表时如何获取数据框列的唯一值-python

python - Pandas :按组对观察结果进行排序

python LinearRegression进行实时预测