- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个有助于简化研究工作的工具,似乎需要检测一列中的数据何时具有递增序列,而另一列中的数据何时具有递增/降序序列。
有没有一种干净的方法来检查行中是否有序列,而不必编写像这样遍历行的状态机 https://stackoverflow.com/a/52679427/5045375 ?编写这样一段代码必须检查一列中的值是否在递增(无间隙),而另一列中的值是否为 asc/desc(无间隙)。我完全能够做到这一点,我只是想知道我的 Pandas 工具箱中是否有我遗漏的东西。
这里有一些例子来阐明我的意图,
import pandas as pd
from collections import namedtuple
QUERY_SEGMENT_ID_COLUMN = 'Query Segment Id'
REFERENCE_SEGMENT_ID_COLUMN = 'Reference Segment Id'
def dataframe(data):
columns = [QUERY_SEGMENT_ID_COLUMN, REFERENCE_SEGMENT_ID_COLUMN]
return pd.DataFrame(data, columns=columns)
# No sequence in either column. No results
data_without_pattern = [[1, 2], [7, 0], [3, 6]]
# Sequence in first column, but no sequence in second column. No results
data_with_pseodo_pattern_query = [[1, 2], [2, 0], [3, 6]]
# Sequence in second column, but no sequence in first column. No results
data_with_pseudo_pattern_reference = [[1, 2], [7, 3], [3, 4]]
# Broken sequence in first column, sequence in second column. No results
data_with_pseudo_pattern_query_broken = [[1, 2], [3, 3], [7, 4]]
# Sequence occurs in both columns, asc. Expect results
data_with_pattern_asc = [[1, 2], [2, 3], [3, 4]]
# Sequence occurs in both columns, desc. Expect results
data_with_pattern_desc = [[1, 4], [2, 3], [3, 2]]
# There is a sequence, and some noise. Expect results
data_with_pattern_and_noise = [[1, 0], [1, 4], [1, 2], [1, 3], [2, 3], [3, 4]]
print(dataframe(data_without_pattern))
Query Segment Id Reference Segment Id
0 1 2
1 7 0
2 3 6
print(dataframe(data_with_pseodo_pattern_query))
Query Segment Id Reference Segment Id
0 1 2
1 2 0
2 3 6
print(dataframe(data_with_pseudo_pattern_reference))
Query Segment Id Reference Segment Id
0 1 2
1 7 3
2 3 4
print(dataframe(data_with_pseudo_pattern_query_broken))
Query Segment Id Reference Segment Id
0 1 2
1 3 3
2 7 4
print(dataframe(data_with_pattern_asc))
Query Segment Id Reference Segment Id
0 1 2
1 2 3
2 3 4
print(dataframe(data_with_pattern_desc))
Query Segment Id Reference Segment Id
0 1 4
1 2 3
2 3 2
print(dataframe(data_with_pattern_and_noise))
Query Segment Id Reference Segment Id
0 1 0
1 1 4
2 1 2
3 1 3
4 2 3
5 3 4
q=(1, 3), r=(2, 4)
的东西。 ,例如
(start, end)
来自相应列(不是索引)的值。
group_by
序列行动,但我没有只见树木不见森林。
data_with_multiple_contiguous_sequences = [[1, 1], [2, 2], [3, 3], [0, 4], [1, 5], [2, 6], [3, 7], [4, 8]]
Query Segment Id Reference Segment Id
0 1 1
1 2 2
2 3 3
3 0 4
4 1 5
5 2 6
6 3 7
7 4 8
q1=(1, 3), r1=(1, 3), q2=(0, 4), r2=(4, 8)
.
最佳答案
如果我理解正确的话,您的问题是孤岛问题的变体。每个具有可接受间隙的单调(增加或减少)子序列将形成一个岛。例如,给定一个系列 s
:
s island
-- ------
0 1
0 1
1 1
3 2 # gap > 1, form new island
4 2
2 3 # stop increasing, form new island
1 3
0 3
Query Segment Id
上应用此间隙岛算法和
Reference Segment Id
:
Query Segment Id Q Island Reference Segment Id R Island Q-R Intersection
---------------- -------- -------------------- -------- ----------------
1 1 1 1 (1, 1)
2 1 2 1 (1, 1)
3 1 3 1 (1, 1)
0 2 4 1 (2, 1)
1 2 5 1 (2, 1)
2 2 6 1 (2, 1)
3 2 7 1 (2, 1)
4 2 8 1 (2, 1)
0 3 9 1 (3, 1)
q
和
r
您正在寻找的范围现在是
Query Segment Id
和
Reference Segment Id
在每个
Q-R Intersection
的开头和结尾.但最后一个警告:忽略长度为 1 的交集(如最后一个交集)。
columns = ['Query Segment Id', 'Reference Segment Id']
df = pd.DataFrame(data_with_multiple_contiguous_sequences, columns=columns)
def get_island(col):
return (~col.diff().between(-1,1)).cumsum()
df[['Q Island', 'R Island']] = df[['Query Segment Id', 'Reference Segment Id']].apply(get_island)
result = df.groupby(['Q Island', 'R Island']) \
.agg(**{
'Q Start': ('Query Segment Id', 'first'),
'Q End': ('Query Segment Id', 'last'),
'R Start': ('Reference Segment Id', 'first'),
'R End': ('Reference Segment Id', 'last'),
'Count': ('Query Segment Id', 'count')
}) \
.replace({'Count': 1}, {'Count': np.nan}) \
.dropna()
result['Q'] = result[['Q Start', 'Q End']].apply(tuple, axis=1)
result['R'] = result[['R Start', 'R End']].apply(tuple, axis=1)
Q Start Q End R Start R End Count Q R
Q Island R Island
1 1 1 3 1 3 3 (1, 3) (1, 3)
2 1 0 4 4 8 5 (0, 4) (4, 8)
关于python - 在 Pandas 数据框中查找 asc/desc 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61175786/
我有一个小程序,我不想再在浏览器中运行,而是想通过 JNLP 作为可下载、自动更新、可离线的 Java 应用程序运行。 我不想保留将其作为小程序运行的能力。 无论是将现有的小程序包装在 JFrame
我认为我没有做任何过于复杂的事情。我正在对一个大型 csv 数据文件进行预排序,因为它充满了按随机时间顺序到达的数据。索引正确,但返回格式不正确。 sortedList=sorted(read
我在GraphicalLayout中添加了一个ImageView,但它在我的实际设备上看起来与在AVD上不同。。我发现我需要在一个布局.xml中添加Content Description,但是当我添加
表格如下: CREATE TABLE `api_stats` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ip` varchar(30) DEFAULT NU
我的表有百万条记录。我只想提取最后 200 行中计数器最多的 5 行。 我正在使用它,但我认为这是两次选择.. 这可能不适合最佳性能,请提出建议。 "select * from (
我正在尝试为 hg log 创建一个自定义模板,其中一部分显示第一行的前 N(例如 72)个字符。基于this answer到目前为止我收到的另一个问题: hg log --template '{de
这里我们有一个带连字符的字符串,如 0-1-3.... 并且长度不固定,我们在hive中还有一个DETAIL表来解释每个代码的含义。 详情 |代码 |描述 | + ---- + ---- + | 0
它应该列出一个列表,并在顶部列出最高百分比..但它没有 https://gyazo.com/ecde864ef09115b8b119eba8a39ecd68这是我运行时的图片 这里是代码。怎么了? $
我正在尝试根据论坛 ID 获取所有主题。这些主题需要首先按粘性排序,然后按最后发布日期排序。 我有这个查询,工作得几乎正常,但它没有按照我想要的方式对主题进行排序。 SELECT forum_
我在数据库中创建了一个表并添加了一些约束。当我在表上使用 DESC 关键字时,根据我放置的约束,“Key”列会更改其记录。 这是表定义: CREATE TABLE t( sif INT, sif
我有一个获取两行的查询 - SELECT tb.type, tb.value, tb.comp, tb.
我在 SQL 中有这个查询 SELECT Book FROM Library ORDER BY Book desc 我想要的排序 [Expired]Web Publishing Co., Ltd. [
我正在使用 DataTables 来显示一些数据,它效果很好,但我想稍微自定义它,但不确定如何。 我想要做的是,当用户单击列标题对该列进行排序时,我希望它最初按降序而不是升序排序。有什么办法可以做到这
我有这个 SQL(SQLite 数据库),它正确地从按 session ID 分组的消息表中获取最新消息,其中消息的 session ID 不是“无”。 SELECT * from messages
我想要 desc 表;操作以显示列的注释。我已经看到有些人做到了这一点,但我不知道如何。也许这取决于 SQL Developer 版本,我的是 2.1.0.63。数据库是Oracle 11g。 这是我
我正在检索 SQL 中的一些数据,按 DESC 排序。然后我想反转结果。我是通过将数据插入数组然后使用 array_reverse 来做到这一点的,但我发现这对 CPU 时间来说非常繁重,并且只想使用
我正在按日期降序从数据库中获取一些数据,并使用shift-push通过循环将其添加到数组中,因为新数据不断定期到达。 我无法理解的一件奇怪的事情是为什么第一个获取的值被添加到最后一个索引。 这是我的代
我有一个问题,我不知道如何解决。我有这个查询: $sql = "SELECT * FROM transfer_centre WHERE status = 'out' ORDER BY pris DES
我有 2 个 MYSQL 表,firma 和 rach 公司表: id_fir | nazwa | opis | nr_konta | logo-------------------------
我希望得到一些帮助:我正在尝试输出一个用户可以通过各种选择字段查看和排列的 mysql 数据表。我希望数据按日期顺序自动排列(最新的在前)并且每页限制为 10 个结果。 我有以下脚本摘录(为简洁起见,
我是一名优秀的程序员,十分优秀!