生成汇总标签页

2024-04-29 08:10:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一种方法总结一个数据库表,这样共享一个公共I d的行就被总结成一行输出。

我的工具是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              |
+--------------------+--------------------+--------------------+

。。。我想制作一张汇总表,显示每个超市每种水果的价格。空格应该用空字符填充。

+----------+----------+----------+----------+
|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    |
+----------+----------+----------+----------+

我相信文献中称之为“pivot表”或“pivot查询”,但显然是SQLite doesn't support ^{}.(这个问题的解决方案使用了硬编码的LEFT JOINs。这对我来说并不是很有吸引力,因为我事先不知道“column”的名称)

现在,我通过在Python中遍历整个表并累积一个dictdicts,这有点笨拙。我对更好的解决方案持开放态度,不管是用Python还是SQLite,它们都会以表格的形式给出数据。


Tags: applesqlitedate价格解决方案nullbananapivot
2条回答

在python方面,您可以使用一些itertools魔术来重新排列数据:

data = [('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)]

from itertools import groupby, islice
from operator import itemgetter
from collections import defaultdict

stores = sorted(set(row[1] for row in data))
# probably splitting this up in multiple lines would be more readable
pivot = ((fruit, defaultdict(lambda: None, (islice(d, 1, None) for d in data))) for fruit, data in groupby(sorted(data), itemgetter(0)))

print 'Fruit'.ljust(12), '\t'.join(stores)
for fruit, prices in pivot:
    print fruit.ljust(12), '\t'.join(str(prices[s]) for s in stores)

输出:

Fruit        Coles      IGA     Woolw
Apple        1.5        1.7     1.6
Banana       0.5        0.7     0.6
Cherry       5.0        None    None
Date         2.0        None    2.1
Elderberry   None       10.0    None

熊猫套餐能很好地处理这个问题。

>>> 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

一些学习熊猫的IPython笔记本: https://bitbucket.org/hrojas/learn-pandas

希望这会有帮助。
致意
帕特里克·布罗克曼

相关问题 更多 >