I'm trying to create a matrix or a contingency table from a file that has this format:
Species Date Data
1 Dec 3
2 Jan 4
2 Dec 6
2 Dec 3
Result
1 2
Dec 3 9
Jan 4
More that I'd like to know how to turn myfile into an array that numpy w开发者_运维百科ill like. Basically I'm trying to recreate reshape from R
Hope this made sense. ThanksBlockquote
Made some edits so it might make more sense
When other people say "Matrix" you have a dictionary with a two-part key.
The problem is murky, but you have something like this.
matrix = {}
# read input
matrix[ (row,column) ] = data
row_keys = set( r for r,c in matrix.keys() )
col_keys = set( c for r,c in matrix.keys() )
for r in row_keys:
print( r, ":", end=' ' )
for c in col_keys:
print( matrix.get( (r,c), None ), end=' ' )
print( end='\n' )
[This requires from __future__ import print_function
for Python 2.7.]
精彩评论