0

Below is the illustration code

from pylab import *

 a = array([1,2,3])
 b = array([4,5])

What I want test.out has is

1 4
2 5
3

previously, people has given solution of store of 1D numpy arrays of different sizes to txt file rowwise: Saving numpy array to txt file row wise

Then how to save them columnwise?

Of course you can use three array like this

 a = array([1,4])
 b = array([2,5])
 c=array([3])

and save them in row wise however it is not a smart way, when there is a lot of 1D arrays.

5
  • you can convert them to string and use pandas. Commented Dec 12, 2015 at 6:09
  • Open a file, and write the numbers out line by line. Use ordinary Python string formatting to create each line. That is basically what np.savetxt does. Commented Dec 12, 2015 at 6:11
  • I will learn pandas later.@Colonel savetxt only saves arrays with the same sizes @hpaulj Commented Dec 12, 2015 at 6:18
  • That's why I suggest doing your own write. I mention savetxt to make it clear that a homespun writer is just as good - better if it handles your problem. This is just an ordinary Python file write problem. Commented Dec 12, 2015 at 6:24
  • no need to write your own write ... pandas is just simplifying life a lot ;) Commented Dec 12, 2015 at 6:36

2 Answers 2

2

A simple approach with pandas:

import pandas as pd

d  = dict(a=a, b=b)
df = pd.DataFrame.from_dict(d, orient='index').transpose().fillna('')

#   a  b
#0  1  4
#1  2  5
#2  3   

And write in a csv (you do not want to write the index nor the column):

df.to_csv('file.csv', index=False, header=False)
Sign up to request clarification or add additional context in comments.

6 Comments

I am learning how to install pandas now, seemingly that .whl is difficult to install than numpy.exe etc.
you can directly install Anaconda continuum.io/downloads, it's python including most of the needed library (numpy, pandas, etc)
I succeeded to install it using pypi, but Why i got this 4 5 is the first column? 1 2 3 second? ...confused
Somehow, the sequence of columns is not fixed as a and b... Is it because the pandas package is installed improperly?
what is your result? you can reorder the columns if needed.
|
2

My answer in https://stackoverflow.com/a/34242952/901925 could be adapted to this case

3 arrays of different length; could be lists

In [715]: heights=np.array([40,50])
In [716]: widths=np.array([60,65,70])
In [717]: yrs=np.array([1995,1996,1997,1998])

use itertools.zip_longest to iterate through them, with a fillvalue where values are missing:

In [718]: for xyz in itertools.zip_longest(yrs,widths,heights,fillvalue=''):
    print('%-12s,%-12s,%-12s'%xyz)
   .....:     
1995        ,60          ,40          
1996        ,65          ,50          
1997        ,70          ,            
1998        ,            ,  

to write to a file use:

In [719]: with open('temp.txt','w') as f:
    for xyz in itertools.zip_longest(yrs,widths,heights,fillvalue=''):
        f.write('%-12s,%-12s,%-12s\n'%xyz)
   .....:         
In [720]: cat temp.txt
1995        ,60          ,40          
1996        ,65          ,50          
1997        ,70          ,            
1998        ,            ,  

(itertools.zip_longest in PY3, izip_longest in Py2).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.