[SciPy-user] Save column & row headers to csv filewith something like savetxt?

Dharhas Pothina Dharhas.Pothina@twdb.state.tx...
Mon Dec 22 10:50:35 CST 2008


Alan,

Your example below is basically what I need to do but it doesn't seem to work correctly. Only the first 3 of my 5 columns are being output. 

Output using SimpleTable :

,"2003_C142","JulSep03_CS142","AS03_C142"
"SL",2.53231931381,3.94970820455,2.79171928058
"WL",0.664414140959,0.640946023274,0.353328693939
"CS",0.709166978019,0.682718096976,0.367865305274

Output using np.savetxt :

2.53,3.95,2.79,3.16,2.70
0.66,0.64,0.35,0.97,0.24
0.71,0.68,0.37,0.85,0.18

My code is below. Please let me know if I am using SimpleTable incorrectly.

thanks,

- dharhas

Code :
-----------------------------
table = np.zeros((stations.size,3,5))
headers = "2003_C142","JulSep03_CS142","AS03_C142","AS03_C284","AS03_LDY142"
rows = "SL","WL","CS"
for i in range(stations.size):
    site=stations[i]

    #2003
    table[i,0,0] = np.mean(base.data[:,0]) - np.mean(SL_2003_142.data[:,i,0,0]) 
    table[i,1,0] = np.mean(base.data[:,i,0,0]) - np.mean(WL_2003_142.data[:,i,0,0])
    table[i,2,0] = np.mean(base.data[:,i,0,0]) - np.mean(CS_2003_142.data[:,i,0,0])

   #2003 Jul-Sept
    table[i,0,1] = np.mean(base.data[4337:6545,i,0,0]) - np.mean(SL_2003_142.data[4337:6545,i,0,0]) 
    table[i,1,1] = np.mean(base.data[4337:6545,i,0,0]) - np.mean(WL_2003_142.data[4337:6545,i,0,0])
    table[i,2,1] = np.mean(base.data[4337:6545,i,0,0]) - np.mean(CS_2003_142.data[4337:6545,i,0,0])

    table[i,0,2] = np.mean(baseAS.data[:,i,0,0]) - np.mean(SL_AS03_142.data[:,i,0,0]) 
    table[i,1,2] = np.mean(baseAS.data[:,i,0,0]) - np.mean(WL_AS03_142.data[:,i,0,0])
    table[i,2,2] = np.mean(baseAS.data[:,i,0,0]) - np.mean(CS_AS03_142.data[:,i,0,0])

    table[i,0,3] = np.mean(baseAS.data[:,i,0,0]) - np.mean(SL_AS03_284.data[:,i,0,0]) 
    table[i,1,3] = np.mean(baseAS.data[:,i,0,0]) - np.mean(WL_AS03_284.data[:,i,0,0])
    table[i,2,3] = np.mean(baseAS.data[:,i,0,0]) - np.mean(CS_AS03_284.data[:,i,0,0])

    table[i,0,4] = np.mean(baseAS.data[:,i,0,0]) - np.mean(SL_AS03_LDY.data[:,i,0,0]) 
    table[i,1,4] = np.mean(baseAS.data[:,i,0,0]) - np.mean(WL_AS03_LDY.data[:,i,0,0])
    table[i,2,4] = np.mean(baseAS.data[:,i,0,0]) - np.mean(CS_AS03_LDY.data[:,i,0,0])

    tbl = text.SimpleTable(table[i,:,:],headers,rows)
    fh = open(site + '_means_table.csv','w')
    fh.write( tbl.as_csv() )
    np.savetxt(site + '_means_table.txt', table[i,:,:], delimiter=',', fmt='%4.2f')

----------------------------------

>>> Alan G Isaac <aisaac@american.edu> 12/21/08 3:15 PM >>>
SimpleTable now supports column specific formatting.
Example below.

fwiw,
Alan Isaac

     >>> mydata = [[11,12],[21,22]]  #can be 2d array
     >>> myheaders = "Column 1", "Column 2"
     >>> mystubs = "Row 1", "Row 2"
     >>> tbl = text.SimpleTable(mydata, myheaders, mystubs, title="Title")
     >>> print tbl
              Title
     =======================
           Column 1 Column 2
     -----------------------
     Row 1    11       12
     Row 2    21       22
     -----------------------

     >>> # set column specific data formatting
     ... tbl = text.SimpleTable(mydata, myheaders, mystubs,
     ...     fmt={'data_fmt':["%3.2f","%d"]})
     >>> print tbl

     =======================
           Column 1 Column 2
     -----------------------
     Row 1  11.00      12
     Row 2  21.00      22
     -----------------------
     >>> print tbl.as_csv()

     ,"Column 1","Column 2"
     "Row 1",11.00,12
     "Row 2",21.00,22


_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list