- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想要一种汇总数据库表的方法,以便将共享公共(public) ID 的行汇总到一行输出中。
我的工具是 SQLite 和 Python 2.x。
例如,给出下表我本地超市的水果价格...
+--------------------+--------------------+--------------------+
|Fruit |Shop |Price |
+--------------------+--------------------+--------------------+
|Apple |Coles |$1.50 |
|Apple |Woolworths |$1.60 |
|Apple |IGA |$1.70 |
|Banana |Coles |$0.50 |
|Banana |Woolworths |$0.60 |
|Banana |IGA |$0.70 |
|Cherry |Coles |$5.00 |
|Date |Coles |$2.00 |
|Date |Woolworths |$2.10 |
|Elderberry |IGA |$10.00 |
+--------------------+--------------------+--------------------+
...我想生成一个汇总表,显示每个超市每种水果的价格。空格应由 NULL 填充。
+----------+----------+----------+----------+
|Fruit |Coles |Woolworths|IGA |
+----------+----------+----------+----------+
|Apple |$1.50 |$1.60 |$1.70 |
|Banana |$0.50 |$0.60 |$0.70 |
|Cherry |NULL |$5.00 |NULL |
|Date |$2.00 |$2.10 |NULL |
|Elderberry|NULL |NULL |$10.00 |
+----------+----------+----------+----------+
我相信文献称其为“数据透视表”或“数据透视查询”,但显然 SQLite doesn't support PIVOT
. (该问题中的解决方案使用硬编码的 LEFT JOIN
。这对我来说并没有真正的吸引力,因为我事先不知道“列”名称。)
现在我通过在 Python 中遍历整个表并累积 dicts
的 dict
来完成此操作,这有点笨拙。我愿意接受更好的解决方案,无论是在 Python 还是 SQLite 中,它们都会以表格形式提供数据。
最佳答案
pandas 包可以很好地处理这个问题。
>>> import pandas
>>> df=pandas.DataFrame(data, columns=['Fruit', 'Shop', 'Price'])
>>> df.pivot(index='Fruit', columns='Shop', values='Price')
Shop Coles IGA Woolworths
Fruit
Apple 1.5 1.7 1.6
Banana 0.5 0.7 0.6
Cherry 5.0 NaN NaN
Date 2.0 NaN 2.1
Elderberry NaN 10.0 NaN
文档: http://pandas.pydata.org/pandas-docs/stable/reshaping.html
一些用于学习 pandas 的 IPython 笔记本: https://bitbucket.org/hrojas/learn-pandas
希望对您有所帮助。
问候
帕特里克布罗克曼
关于python - 生成一个摘要 ("pivot"?) 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428271/
我有一个 todolist 应用程序,它在 Pivot 控件内的单独 PivotItems 中具有多个 ListBox 控件。如果我导航到另一个页面,然后使用后退按钮返回,则显示的 Pivot 没有响
我想 pivot_long() 下面数据集的多列避免硬编码。我看过一些类似的问题,但我仍然做不到。 宽数据: > head(data) ID IND_TEST_SCORE ARG_G1_A
假设我有一个 orders 表,它将与另外三个名为 typings、translates 和 论文 。我知道数据透视表应该有点像 many to many polymorphic relation但这
当我尝试将 null 替换为 zero 时,收到以下错误消息。 The column name "jan" specified in the PIVOT operator conflicts with
有没有办法在数据透视表中为计算为零的单元格隐藏或显示空白单元格? 最佳答案 使用数字格式隐藏所选单元格中的零值: 按照此过程隐藏所选单元格中的零值。如果其中一个单元格中的值更改为非零值,则该值的格式将
我正在尝试理解 Select algorithm我遇到了 a good pivot VS a bad pivot 。我可以看到该算法正在使用 Partition 算法来分隔右侧的较大元素pivot 和
我有以下代码:
我有一个国家表和一个数据透视表 Country_language,其中列出了所有国家及其可用语言的翻译。 表结构如下: Languages -------------- ID Locale Recor
目前,PWS 上唯一的 RabbitMQ 服务看起来有点不确定。我想知道我是否可以使用 Pivotals 解决方案 https://network.pivotal.io/products/p-redi
我是使用 Spark 数据帧的新手。我正在尝试将 pivot 方法与 Spark(Spark 版本 2.x)一起使用并遇到以下错误: Py4JError: An error occurred whil
我需要转置一个表,其中 column1 是实体的名称,column2 到 column366 是一年中包含美元金额的日期。表,select语句,输出结果都给了 以下 - 问题 - 此语法要求我创建一个
我想知道是否可以像在 python 和 R 上那样在 OpenRefine 上创建值的聚合和汇总?示例: 包含 30 万条记录的医疗预约表身份识别患者 |年龄 |身份证预约 |值 患者汇总和总结的结果
我想知道是否可以像在 python 和 R 上那样在 OpenRefine 上创建值的聚合和汇总?示例: 包含 30 万条记录的医疗预约表身份识别患者 |年龄 |身份证预约 |值 患者汇总和总结的结果
我不熟悉 SQL 并使用 Google BigQuery。我有一个表,其中有一条记录如下所示: publication_number |受让人 US-6044964-A|索尼公司 |数字音频光盘公司
在尝试转换 sql 表时,我看到了这篇文章 Here .通过使用这种方法,我创建了一个查询。但是我现在已经意识到它当然会使用 MAX 函数聚合结果。但是,我需要 Colum 旋转,但要显示所有事件。从
我们能否将行旋转到多列,即 Create table #Temp_Trans ( P_ID int, Custom_Name varchar(30), Text_Value var
计算字段很棒,但有一些限制,例如无法对其进行排序(无法将字段移动到报告过滤器区域)。 我试过“强制”一个过滤器,就像微软的人建议的那样:https://answers.microsoft.com/en
我有以下数据集,我需要从中计算数据透视中不同值的计数。我尝试了几个函数,如 FREQUENCY、COUNTIFS 等,但我做不到。 输入 Input Data 输出 Expected Output 最
请看下面的数据: 我正在寻找生成以下输出的查询: 我正在试验“PIVOT”,但尚未取得预期的结果。 最佳答案 这应该有效: SELECT ReviewType, DER, LEI, NOR, [NOT
我有两个表Person(person_id, name) 和另一个表Contacts(person_id, phone_type, phone_no)。 Person-----------------
我是一名优秀的程序员,十分优秀!