2012-09-30 35 views
0

我的數據集是一個或者一起工作或者單獨工作的人員列表。製作各種分組

我對每個項目都有一行,並且列出了所有在該項目上工作的人員的姓名。如果第2列是連續第一個空列,那麼這是一個獨奏作業。如果第4列是連續第一個空列,則有3個人一起工作。

我有代碼找到所有對。在輸出數據集中,每個角色標註行和列都會創建一個正方形N x N。單元格(A,B)和(B,A)包含該對單元一起工作的次數。與B A工作被視爲與B相同與A.

輸入數據的一個例子的工作,在一個逗號分隔的方式:

A,.,. 
A,B,. 
B,C,E 
B,F,. 
D,F,. 
A,B,C 
D,B,. 
E,C,B 
X,D,A 
F,D,. 
B,.,. 
F,.,. 
F,X,C 
C,F,D 

我使用Python 3.2。執行此代碼:

import csv 
import collections 
import itertools 

grid = collections.Counter() 

with open("connect.csv", "r") as fp: 
    reader = csv.reader(fp) 
    for line in reader: 
     # clean empty names 
     line = [name.strip() for name in line if name.strip()] 
     # count single works 
     if len(line) == 1: 
      grid[line[0], line[0]] += 1 
     # do pairwise counts 
     for pair in itertools.combinations(line, 2): 
      grid[pair] += 1 
      grid[pair[::-1]] += 1 

actors = sorted(set(pair[0] for pair in grid)) 

with open("connection_grid.csv", "w") as fp: 
    writer = csv.writer(fp) 
    writer.writerow([''] + actors) 
    for actor in actors: 
     line = [actor,] + [grid[actor, other] for other in actors] 
     writer.writerow(line) 

我的問題是:

  1. 如果我有幾個月甚至幾年一列,是有可能使每個月一年矩陣表格? (即2011年,我會有12個矩陣)?

  2. 對於我使用的任何故障,是否可以創建一個變量,以便變量名是所有一起工作的人的組合?例如'ABD'意味着一個項目A人,B人和D人一起工作,並且等於ABD按照任意順序作爲一個三人小組工作的次數。項目最多可容納20人,因此它必須能夠組成2到20個小組。另外,如果變量應按字母順序排列,這將是最容易的。

+0

快速推薦/請求。請返回並更正「打開」代碼塊中的縮進。如您所知,縮進在Python中很重要。沒有他們,讀者就會猜測你在做什麼。 –

回答

1

1)按月&年排序您的項目,然後創建每個月都有新的「網格」。例如:

從每一行拉年。從行中刪除月份&年,然後將其餘數據添加到字典中。最後你得到類似於{(,):[,,...]}。從那裏,很容易循環每個月/每年,並創建一個網格,輸出電子表格等。

2)''.join(sorted(list)).replace('.','')給你一起工作的人按字母順序列出。

import csv 
import collections 
import itertools 

grids = dict() 
groups = dict() 

with open("connect.csv", "r") as fp: 
    reader = csv.reader(fp) 
    for line in reader: 
     # extract month/year from the last column 
     date = line.pop(-1) 
     month,year = date.split('/') 
     # clean empty names 
     line = [name.strip() for name in line if name.strip()] 
     # generate group name 
     group = ''.join(sorted(line)).replace('.','') 
     #increment group count 
     if group in groups: 
      groups[group]+=1 
     else: 
      groups[group]=1 
     #if grid exists for month, update else create 
     if (month,year) in grids: 
      grid = grids[(month,year)] 
     else: 
      grid = collections.Counter() 
      grids[(month,year)] = grid 
     # count single works 
     if len(line) == 1: 
      grid[line[0], line[0]] += 1 
     # do pairwise counts 
     for pair in itertools.combinations(line, 2): 
      grid[pair] += 1 
      grid[pair[::-1]] += 1 

for date,grid in grids.items(): 
    actors = sorted(set(pair[0] for pair in grid)) 
    #Filename from date 
    filename = "connection_grid_%s_%s.csv" % date 
    with open(filename, "w") as fp: 
     writer = csv.writer(fp) 
     writer.writerow([''] + actors) 
     for actor in actors: 
      line = [actor,] + [grid[actor, other] for other in actors] 
      writer.writerow(line) 

with open('groups.csv','w') as fp: 
    writer = csv.writer(fp) 
    for item in sorted(groups.items()): 
     writer.writerow(item) 
+0

這太好了,我有幾天,所以我添加到您的代碼'月,日,年'..可以添加,如果你喜歡。一個問題是,我在我的行和列之間得到空格,無法擺脫它們。通過變量,我應該已經更清楚了,我想要一個電子表格,列出所有的分組(按照字母順序),並且旁邊的列將會顯示該分組在數據中發生了多少次。因此,對於每個分組給出的數據是唯一的,保存一個,所以我會(用一個,分割輸出列)A,1; AB,1; BCE,2(因爲BCE和ECB相當); BF,1; etc – FJ17

+0

另外,我怎樣才能標記文件名稱與年份然後一個月,因爲我有多年的數據 – FJ17

+0

好吧,我編輯代碼打印出組到csv也。 它已經按月份生成文件名了。反之亦然,請將上面的代碼切換爲(年,月)而不是(月,年),月份除外,year = date.split('/') 我不確定爲什麼要在你的行和列,需要查看輸入文件。 我希望你使用SO來學習,不要讓自定義腳本完成!上面的腳本完成了你所需要的大部分工作,你應該自己嘗試一下,讓它按照你的要求工作。提示:爲了消除額外的空間,我們使用.strip() –