矩阵的标题

在以下数据集中,我需要添加列和行,以便我知道例如员工“12”从雇主“a”到雇主“b”的位置。这是我的数据集


employerEmployeeEdges = [(12, 'a'), (15, 'a'), (17, 'a'), (12, 'a'), (15, 'a'), (23, 'b'), (12, 'b'), (18, 'b'), (12, 'b'), (12, 'b'), (15, 'a'), (12, 'a'), (15, 'a'), (15, 'a'), (24, 'c'), (12, 'c')]


employerEmployeeEdges=np.array(employerEmployeeEdges)

#print(employerEmployeeEdges)


unique_employee = np.unique(employerEmployeeEdges[:,1])

n_unique = len(unique_employee)

#print(unique_employee)



Q = np.zeros([n_unique,n_unique])


for n, employer_employee in enumerate(employerEmployeeEdges):

    #print(employer_employee)

    #copy the array for the original o be intact

    eee = np.copy(employerEmployeeEdges)

    #sustitue the current tuple with a empty one to avoid self comparing

    eee[n] = (None,None)

    #get the index for the current employee, the one on the y axis

    employee_index = np.where(employer_employee[1] == unique_employee)

    #get the indexes where the the employees letter match

    eq_index = np.where(eee[:,0] == employer_employee[0])[0]

    eq_employee = eee[eq_index,1]

    #add at the final array Q by index

    for emp in eq_employee:

        print(np.unique(emp))

        emp_index = np.where(unique_employee == emp)

        #print(emp)

        Q[employee_index,emp_index]+= 1

        #df = pd.DataFrame(Q, columns=emp, index=emp)


print(Q) 


[[26.  9.  3.]

 [ 9.  6.  3.]

 [ 3.  3.  0.]]

我想在上面的这个矩阵中添加列和行标题


这是我到目前为止所做的:


for index, row in enumerate(Q):

    if index < len(Q)-1:

        print('{}\t'.format(str(index + 1))),

    else:

        print(' \t'),

    print('|'.join('{0:.2f}'.format(x) for x in row))


1   26.00|9.00|3.00

2   9.00|6.00|3.00

    3.00|3.00|0.00

由于某种原因,我无法向数组添加列或行。我需要做什么?这个数组应该看起来像(我想要的输出)


       a    b    c

a   26.00|9.00|3.00

b   9.00|6.00|3.00

b   3.00|3.00|0.00

基于安德鲁的帮助,这里是解决方案


df = pd.DataFrame(Q)

df.index = unique_employee

df.columns = unique_employee

print(df)

      a    b    c

a  26.0  9.0  3.0

b   9.0  6.0  3.0

c   3.0  3.0  0.0


aluckdog
浏览 179回答 1
1回答

慕沐林林

您可以使用熊猫并指定index(行标签)和columns(列标签)来匹配您的unique_employee数组。import pandas as pd&nbsp;print(Q)&nbsp;[[26.&nbsp; 9.&nbsp; 3.]&nbsp;[ 9.&nbsp; 6.&nbsp; 3.]&nbsp;[ 3.&nbsp; 3.&nbsp; 0.]]df = pd.DataFrame(Q)df.index = unique_employeedf.columns = unique_employeeprint(df)&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; b&nbsp; &nbsp; ca&nbsp; 26.0&nbsp; 9.0&nbsp; 3.0b&nbsp; &nbsp;9.0&nbsp; 6.0&nbsp; 3.0c&nbsp; &nbsp;3.0&nbsp; 3.0&nbsp; 0.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python