python-将矩阵转换为数据帧
我创建了一个矩阵:python-将矩阵转换为数据帧,python,pandas,numpy,Python,Pandas,Numpy,我创建了一个矩阵: items = [0, 1, 2, 3] item_to_item = pd.DataFrame(index=items, columns=items) 我在其中加入了价值观,因此: 其对称性 它的对角线都是0 例如: 0 1 2 3 0 0 4 5 9 1 4 0 3 7 2 5 3 0 3 3 9 7 3 0 我想创建一个包含所有可能对(从[0,1,2,3])的数据帧,这样就不会有(x,x)对,如果(x,y)在,我不想要(y
items = [0, 1, 2, 3]
item_to_item = pd.DataFrame(index=items, columns=items)
我在其中加入了价值观,因此:
0 1 2 3
0 0 4 5 9
1 4 0 3 7
2 5 3 0 3
3 9 7 3 0
我想创建一个包含所有可能对(从[0,1,2,3])的数据帧,这样就不会有(x,x)
对,如果(x,y)
在,我不想要(y,x)
,因为它的符号和值相同。
最后,我将拥有以下数据帧(或numpy 2d数组)
numpy的np.triu为您提供了上三角形,所有其他元素都设置为零。您可以使用它来构造数据帧,并将其替换为NAN(以便在堆叠列时删除它们): 您可以在末尾使用
reset_index
将索引转换为列
另一种选择是重新设置索引并再次堆叠,但这次使用可调用的方法对数据帧进行切片:
df.stack().reset_index()[lambda x: x['level_0'] < x['level_1']]
Out:
level_0 level_1 0
1 0 1 4
2 0 2 5
3 0 3 9
6 1 2 3
7 1 3 7
11 2 3 3
df.stack().reset_index()[lambda x:x['level_0']
这一个需要pandas 0.18.0。numpy的np.triu为您提供了上三角形,所有其他元素都设置为零。您可以使用它来构造数据帧,并将其替换为NAN(以便在堆叠列时删除它们): 您可以在末尾使用
reset_index
将索引转换为列
另一种选择是重新设置索引并再次堆叠,但这次使用可调用的方法对数据帧进行切片:
df.stack().reset_index()[lambda x: x['level_0'] < x['level_1']]
Out:
level_0 level_1 0
1 0 1 4
2 0 2 5
3 0 3 9
6 1 2 3
7 1 3 7
11 2 3 3
df.stack().reset_index()[lambda x:x['level_0']
这一个需要pandas 0.18.0。这是一个带有-
这里有一个简单的解决方案-
你知道我如何保存原始身份证吗?我注意到它们正在从0改为len(items)@eranmose。如果你想使用行索引,那么你可以这样做:
np.column\u堆栈((item\u to\u item.index[r],item\u to\u item.index[c],…)
。让我知道它是否对你有效。非常有效,我的朋友!你知道我如何保存原始身份证吗?我注意到它们正在从0改为len(items)@eranmose。如果你想使用行索引,那么你可以这样做:np.column\u堆栈((item\u to\u item.index[r],item\u to\u item.index[c],…)
。如果对你有用,告诉我。我的朋友干得好!
df.stack().reset_index()[lambda x: x['level_0'] < x['level_1']]
Out:
level_0 level_1 0
1 0 1 4
2 0 2 5
3 0 3 9
6 1 2 3
7 1 3 7
11 2 3 3
In [453]: item_to_item
Out[453]:
0 1 2 3
0 0 4 5 9
1 4 0 3 7
2 5 3 0 3
3 9 7 3 0
In [454]: r,c = np.triu_indices(len(items),1)
In [455]: pd.DataFrame(np.column_stack((r,c, item_to_item.values[r,c])))
Out[455]:
0 1 2
0 0 1 4
1 0 2 5
2 0 3 9
3 1 2 3
4 1 3 7
5 2 3 3