заголовки матрицы - PullRequest
       2

заголовки матрицы

0 голосов
/ 17 октября 2018

В следующем наборе данных мне нужно добавить столбец и строку, чтобы я знал, куда, например, работник «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

Iне могу по какой-то причине добавить столбцы или строки в массив.Что мне нужно сделать?Этот массив должен выглядеть следующим образом (мой желаемый результат)

       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

1 Ответ

0 голосов
/ 17 октября 2018

Вы можете использовать панд и указать index (метки строк) и columns (метки столбцов), чтобы соответствовать вашему массиву unique_employee.

import pandas as pd 

print(Q) 
[[26.  9.  3.]
 [ 9.  6.  3.]
 [ 3.  3.  0.]]

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...