将N x N数据帧转换为3列数据帧 [英] Convert N by N Dataframe to 3 Column Dataframe
问题描述
我在Windows 10机器上使用Python 2.7和Pandas。
I am using Python 2.7 with Pandas on a Windows 10 machine.
我有n×n Dataframe
其中:
1)索引表示人员名称
1) The index represents peoples names
2)列标题是相同的人员名称
2) The column headers are the same peoples names in the same order
3)数据框
中的每个单元格是彼此相互发送电子邮件的平均次数
3) Each cell of the Dataframe
is the average number of times they email each other each day.
如何将数据框
转换为数据框
具有3列,其中:
How would I transform that Dataframe
into a Dataframe
with 3 columns, where:
1)第1列是n乘n的索引 Dataframe
1) Column 1 would be the index of the n by n Dataframe
2)第2列是n乘n的行标题 Dataframe
2) Column 2 would be the row headers of the n by n Dataframe
3)第3列将是与索引中这两个名称相对应的单元格值,从n到n Dataframe
3) Column 3 would be the cell value corresponding to those two names from the index, column header combination from the n by n Dataframe
编辑
应用程序未提供我正在寻找的示例。我想从以下代码中将df1转换为rel_df。
Appologies for not providing an example of what I am looking for. I would like to take df1 and turn it into rel_df, from the code below.
import pandas as pd
from itertools import permutations
df1 = pd.DataFrame()
df1['index'] = ['a', 'b','c','d','e']
df1.set_index('index', inplace = True)
df1['a'] = [0,1,2,3,4]
df1['b'] = [1,0,2,3,4]
df1['c'] = [4,1,0,3,4]
df1['d'] = [5,1,2,0,4]
df1['e'] = [7,1,2,3,0]
##df of all relationships to build
flds = pd.Series(SO_df.fld1.unique())
flds = pd.Series(flds.append(pd.Series(SO_df.fld2.unique())).unique())
combos = []
for L in range(0, len(flds)+1):
for subset in permutations(flds, L):
if len(subset) == 2:
combos.append(subset)
if len(subset) > 2:
break
rel_df = pd.DataFrame.from_records(data = combos, columns = ['fld1','fld2'])
rel_df['value'] = [1,4,5,7,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]
print df1
>>> print df1
a b c d e
index
a 0 1 4 5 7
b 1 0 1 1 1
c 2 2 0 2 2
d 3 3 3 0 3
e 4 4 4 4 0
>>> print rel_df
fld1 fld2 value
0 a b 1
1 a c 4
2 a d 5
3 a e 7
4 b a 1
5 b c 1
6 b d 1
7 b e 1
8 c a 2
9 c b 2
10 c d 2
11 c e 2
12 d a 3
13 d b 3
14 d c 3
15 d e 3
16 e a 4
17 e b 4
18 e c 4
19 e d 4
推荐答案
使用融化:
df1 = df1.reset_index()
pd.melt(df1, id_vars='index', value_vars=df1.columns.tolist()[1:])
(如果您在实际代码中像此处一样明确设置了索引,只需跳过该步骤,而不要执行 reset_index
; 融化
不适用于索引。)
(If in your actual code you're explicitly setting the index as you do here, just skip that step rather than doing the reset_index
; melt
doesn't work on an index.)
这篇关于将N x N数据帧转换为3列数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!