- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想对某些数据执行一些pivot
操作。就像关注一样。
>>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
... 'two'],
... 'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
... 'baz': [1, 2, 3, 4, 5, 6],
... 'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
>>> df
foo bar baz zoo
0 one A 1 x
1 one B 2 y
2 one C 3 z
3 two A 4 q
4 two B 5 w
5 two C 6 t
>>> df.pivot(index='foo', columns='bar', values='baz')
bar A B C
foo
one 1 2 3
two 4 5 6
我知道 DolphinDB 可以在 sql 中做 pivot
。
dateValue=2007.08.01
num=500
syms = (exec count(*) from taq
where
date = dateValue,
time between 09:30:00 : 15:59:59,
0<bid, bid<ofr, ofr<bid*1.2
group by symbol order by count desc).symbol[0:num]
priceMatrix = exec avg(bid + ofr)/2.0 as price from taq
where
date = dateValue, Symbol in syms,
0<bid, bid<ofr, ofr<bid*1.2,
time between 09:30:00 : 15:59:59
pivot by time.minute() as minute, Symbol
但是如何在 clickhouse 中做 pivot
呢?我应该使用客户端 API 来获取数据吗?但是行太多了,很多行处理起来太难了。如果我不会使用pandas
,如何轻松实现pivot
操作?
最佳答案
这是可以帮助您起步的初步实现。
备注:
不支持行中的“空洞”(每列应包含值)
所有列的类型转换为普通类型(String)
引入了字段orderNum。是源列在结果中的序号(例如'bar'-column是第2个)
结果表示为具有数组类型的行和一列。数组项的顺序由 orderNum 定义。
准备测试数据:
CREATE TABLE test.pivot_test
(
orderNum Int,
s String,
values Array(String)
) ENGINE = Memory;
INSERT INTO test.pivot_test
VALUES
(1, 'foo', ['one', 'one', 'one', 'two', 'two', 'two']),
(3, 'baz', ['1', '2', '3', '4', '5', '6']),
(4, 'zoo', ['x', 'y', 'z', 'q', 'w', 't']),
(2, 'bar', ['A', 'B', 'C', 'A', 'B', 'C']);
/*
The content of table test.pivot_test:
┌─orderNum─┬─s───┬─values────────────────────────────────┐
│ 1 │ foo │ ['one','one','one','two','two','two'] │
│ 3 │ baz │ ['1','2','3','4','5','6'] │
│ 4 │ zoo │ ['x','y','z','q','w','t'] │
│ 2 │ bar │ ['A','B','C','A','B','C'] │
└──────────┴─────┴───────────────────────────────────────┘
*/
枢轴模拟:
SELECT arrayMap(x -> x.1, arraySort(x -> x.2, groupArray(value_ordernum))) as row
FROM
(
SELECT
(value, orderNum) AS value_ordernum,
value_index
FROM test.pivot_test
ARRAY JOIN
values AS value,
arrayEnumerate(values) AS value_index
/*
The result of execution the nested query:
┌─value_ordernum─┬─value_index─┐
│ ('one',1) │ 1 │
│ ('one',1) │ 2 │
│ ('one',1) │ 3 │
│ ('two',1) │ 4 │
│ ('two',1) │ 5 │
│ ('two',1) │ 6 │
│ ('1',3) │ 1 │
│ ('2',3) │ 2 │
│ ('3',3) │ 3 │
│ ('4',3) │ 4 │
│ ('5',3) │ 5 │
│ ('6',3) │ 6 │
│ ('x',4) │ 1 │
│ ('y',4) │ 2 │
│ ('z',4) │ 3 │
│ ('q',4) │ 4 │
│ ('w',4) │ 5 │
│ ('t',4) │ 6 │
│ ('A',2) │ 1 │
│ ('B',2) │ 2 │
│ ('C',2) │ 3 │
│ ('A',2) │ 4 │
│ ('B',2) │ 5 │
│ ('C',2) │ 6 │
└────────────────┴─────────────┘
*/
)
GROUP BY value_index;
/*
The final result:
┌─row─────────────────┐
│ ['two','A','4','q'] │
│ ['one','C','3','z'] │
│ ['one','B','2','y'] │
│ ['two','B','5','w'] │
│ ['one','A','1','x'] │
│ ['two','C','6','t'] │
└─────────────────────┘
*/
关于database - 如何像在dolphindb中一样在clickhouse中实现 `pivot`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074216/
当我尝试通过 clickhouse 命令行连接到远程 clickhouse db 时:$ clickhouse-client -h some_ip.com --port 8123 -u some_us
1.概述 转载:ClickHouse 内部架构介绍 官方原文链接:https://clickhouse.yandex/docs/en/development/architecture/ ClickHo
我知道 ClickHouse 没有 ACID ,因此我不希望它有 D可用性 ACID性。但是,问题是,如果服务器崩溃,是否有可能丢失插入内容? 最佳答案 CH 不耐用。 您可以在硬件自发重新启动时丢失
我在 clickhouse 中有一个事件表(MergeTree),并且想同时运行很多小插入。然而,服务器变得过载且无响应。此外,一些插入物丢失了。 clickhouse错误日志中有很多记录: 01:4
也许我错过了一些简单的事情,但我无法使时间过滤工作。 这是我的示例查询: select toTimeZone(ts, 'Etc/GMT+2') as z from (select toDateTime
我们的 Clickhouse 服务器在峰值负载下运行小型查询时出现了几个异常: DB::Exception: Too much simultaneous queries. Maximum: 100
也许我错过了一些简单的事情,但我无法使时间过滤工作。 这是我的示例查询: select toTimeZone(ts, 'Etc/GMT+2') as z from (select toDateTime
1.概述 转载:ClickHouse 11.副本与分片 1. 副本 集群是副本和分片的基础,它将 clickhouse 的服务拓扑由单节点延伸到多个节点。 clickhouse 集群配置很灵活,既可以
1.概述 转载:【clickhouse】clickhouse 副本与分片 分片详解 clickhouse 中每个服务器节点都可以被称为一个 shard(分片)。 假设有 N 台服务器,每个服务器上都有
我阅读了类似的问题,可以通过使用窗口函数使其工作,但是,由于 ClickHouse 似乎不支持它们,我正在寻找替代解决方案。 给定像 (1, 5), (2, 3), (3, 8), (10, 15)
我们有一个适度的 clickhouse 集群,大约 30 个节点,并希望收集它的使用统计信息。我们希望使用针对系统表的预定查询来做到这一点,但使用普通查询只能获取您碰巧连接到的一个节点的信息,并且创建
我是 Clickhouse 的新手,正在尝试入门。我已经安装了能够在我的计算机(ubuntu 16.04)上使用它所需的所有软件包,但是当我使用 clickhouse-client 命令时,我得到以下
是否有任何命令/SQL 可以显示 ClickHouse 数据库中的表正在使用什么引擎? create table t (id UInt16, name String) ENGINE = Memory;
我对 Clickhouse 很陌生,我的第一次尝试似乎总是为 SELECT 生成这种输出: :) select * from test SELECT * FROM test ┌─s───┬───i─┐
我在 Windows 主机上的 docker 容器中运行 Clickhouse。我尝试创建一个帐户以使其成为管理员帐户。看起来 默认 用户没有创建其他帐户的权限。如何解决此错误并创建管理员帐户? do
有什么方法可以更改表并更改 clickhouse 中的列名称吗?我只发现更改了表名称,但没有以直接的方式更改单个列。 谢谢。 最佳答案 该功能已推出here进入 v20.4。 ALTER TABLE
我看到 clickhouse 为每个分区键(在每个节点中)创建了多个目录。 文档说目录名称格式是:分区ID_最小块号_最大块号_级别。 知道这里是什么水平吗? 一个节点(一个表)上的 347 个不同的
我对何时使用二级索引感到困惑。我有以下代码脚本来定义 MergeTree 表,该表有十亿行。 create table t_mt( id UInt8, name String, job Stri
我正在编写一个应用程序来绘制财务数据并与此类数据的实时源进行交互。由于任务的性质,可能会以一次一次交易的方式非常频繁地接收实时市场数据。我在本地使用数据库,而且我是唯一的用户。只有一个程序(我的中间件
在回答关于clickhouse的UDF的github ticket中,他们在2017年回答说不能在clickhouse中创建UDF。我想知道2020年现在有什么办法可以做到吗? 最佳答案 ClickH
我是一名优秀的程序员,十分优秀!