如何在Python3中透视/交叉表数据?
Python3中透视/交叉表的最佳解决方案是什么?是否有一个内置函数可以实现这一点?理想情况下,我正在寻找一个没有外部依赖关系的Python3解决方案。例如,给定一个嵌套列表:如何在Python3中透视/交叉表数据?,python,pivot,crosstab,Python,Pivot,Crosstab,Python3中透视/交叉表的最佳解决方案是什么?是否有一个内置函数可以实现这一点?理想情况下,我正在寻找一个没有外部依赖关系的Python3解决方案。例如,给定一个嵌套列表: nl = [["apples", 2 "New York"], ["peaches", 6, "New York"], ["apples", 6, "New York"], ["peaches", 1, "Vermont"]] 我希望能够重新排列行数据和groupby字段:
nl = [["apples", 2 "New York"],
["peaches", 6, "New York"],
["apples", 6, "New York"],
["peaches", 1, "Vermont"]]
我希望能够重新排列行数据和groupby字段:
apples peaches
New York 2 6
Vermont 6 1
上面是一个简单的例子,但是有没有一种解决方案比使用
itertools.groupby
每当需要一个pivot时都更简单?理想情况下,该解决方案将允许在行数据在任何列上旋转。我在讨论如何使用它,但它是一个外部库,并且只有有限的Python 3支持。下面是一些简单的代码。提供行/列/总计留给读者作为练习
class CrossTab(object):
def __init__(
self,
missing=0, # what to return for an empty cell.
# Alternatives: '', 0.0, None, 'NULL'
):
self.missing = missing
self.col_key_set = set()
self.cell_dict = {}
self.headings_OK = False
def add_item(self, row_key, col_key, value):
self.col_key_set.add(col_key)
try:
self.cell_dict[row_key][col_key] += value
except KeyError:
try:
self.cell_dict[row_key][col_key] = value
except KeyError:
self.cell_dict[row_key] = {col_key: value}
def _process_headings(self):
if self.headings_OK:
return
self.row_headings = list(sorted(self.cell_dict.keys()))
self.col_headings = list(sorted(self.col_key_set))
self.headings_OK = True
def get_col_headings(self):
self._process_headings()
return self.col_headings
def generate_row_info(self):
self._process_headings()
for row_key in self.row_headings:
row_dict = self.cell_dict[row_key]
row_vals = [
row_dict.get(col_key, self.missing)
for col_key in self.col_headings
]
yield row_key, row_vals
if __name__ == "__main__":
data = [["apples", 2, "New York"],
["peaches", 6, "New York"],
["apples", 6, "New York"],
["peaches", 1, "Vermont"]]
ctab = CrossTab(missing='uh-oh')
for s in data:
ctab.add_item(row_key=s[2], col_key=s[0], value=s[1])
print()
print('Column headings:', ctab.get_col_headings())
for row_heading, row_values in ctab.generate_row_info():
print(repr(row_heading), row_values)
输出:
Column headings: ['apples', 'peaches']
'New York' [8, 6]
'Vermont' ['uh-oh', 1]
另见
而且,我已经忘记了这一点。
itertools.groupby
正是为解决这个问题而设计的。您将很难找到更好的内容,尤其是在标准库中。“比使用itertools.groupby更简单”?怎么了?你能具体说明你不喜欢它的地方吗?“但它是一种外在的东西”?那有什么不好?你能更清楚地解释为什么这些解决方案是不可接受的吗?如果我有一个包含嵌套值列表的字典,我觉得提取一个特定值并以此为中心不是一件小事。通过“更简单”,我想到了许多统计语言,如SAS或R,它们具有简化此过程的内置函数;例如,R的plyr和重塑软件包,“完全针对这个问题而设计”?想提供一些具体的证据吗?例如工作代码?@JohnMachin:今天晚些时候可以。这里是@bigyllowcactus的一些证据。