如何在python中创建双向表?我在数据集中有2个分类变量,并希望通过创建双向表来查看2个变量之间的关系。谢谢。
答案 0 :(得分:5)
>>> from bidict import bidict
>>> husbands2wives = bidict({'john': 'jackie'})
>>> husbands2wives['john'] # the forward mapping is just like with dict
'jackie'
>>> husbands2wives[:'jackie'] # use slice for the inverse mapping
'john'
您可以使用 pip install bidict 安装它。
编辑:对于您的实际问题 - 如果我理解正确 - 我会使用pandas
:
# data.csv
Gender Height GPA HS GPA Seat WtFeel Cheat
Female 64 2.60 2.63 M AboutRt No 1
Male 69 2.70 3.72 M AboutRt No 2
Female 66 3.00 3.44 F AboutRt No 3
Female 63 3.11 2.73 F AboutRt No 4
Male 72 3.40 2.35 B OverWt No 0
In [1]: import pandas as pd
In [2]: df = pd.read_csv('data.csv', sep = '\s')
In [3]: grouped = df.groupby(['Gender', 'Seat'])
In [4]: grouped.size()
Out[4]:
Gender Seat
Female AboutRt 3
Male AboutRt 1
OverWt 1
dtype: int64
答案 1 :(得分:1)
您可以使用recipe 578224上的Python Cookbook中显示的DoubleDict
。
答案 2 :(得分:0)
假设您不必进行任何插值,您可以使用字典。使用(x, y)
元组作为键,无论您的值是什么值。例如,像这样的一个简单的2x2表:
___0___1___
0 | 0 0.5
1 | 0.5 1
在代码中看起来像这样:
two_way_lookup = {
(0, 0) : 0,
(0, 1) : 0.5,
(1, 0) : 0.5,
(1, 1) : 1
}
print(two_way_lookup.get((0, 1))) # prints 0.5
答案 3 :(得分:0)
标准库中最好的解决方案,如果您的数据中等,可以使用sqlite
内存数据库:http://docs.python.org/2/library/sqlite3.html
答案 4 :(得分:0)
你可以创建类似两级dict的东西(也就是说,这个dict包含两个以相反顺序映射相同数据的dicts:
>>> mappings=[(0, 6), (1, 7), (2, 8), (3, 9), (4, 10)]
>>> view = dict(view1=dict(mappings), view2=dict(reversed(k) for k in mappings))
>>> view
{'view2': {8: 2, 9: 3, 10: 4, 6: 0, 7: 1},
'view1': {0: 6, 1: 7, 2: 8, 3: 9, 4: 10}}
答案 5 :(得分:0)
如果你想要一个自制的,不稳定的解决方案,你可以做这样的事情:
import collections
class BDMap:
def __init__(self):
self.x_table = {}
self.y_table = {}
def get(self, x = None, y = None):
if (x != None) and (y != None):
y_vals = self.x_table[x]
if (y in y_vals):
return (x, y)
elif x != None:
return self.x_table[x]
elif y != None:
return self.y_table[y]
def set(self, x, y):
if isinstance(x, collections.Hashable) and isinstance(y, collections.Hashable):
self.x_table[x] = self.x_table.get(x, list()) + [y]
self.y_table[y] = self.y_table.get(y, list()) + [x]
else:
raise TypeError("unhashable type")
除了带有小数据集的一次性脚本以外的任何其他内容,使用上述方法之一无疑会更好:但