1

I am using Python 3.6.8 and I have been searching for a little white but I still cannot get the proper export to csv that I want.

I have a list of 1D array which I am trying to export into a CSV file. each array has a different size, and I want to export them following a column order, meaning each 1D array will be stored into 1 column in the CSV file.

To simplify my code, it will do something like this:

import pandas as pd

list_of_thing = []
a = np.arange(10)
list_of_thing.append(a)
b = np.arange(5)
list_of_thing.append(b)

csvfile = "./data.csv"
my_df = pd.DataFrame(list_of_thing)
my_df.to_csv(csvfile, index=False, header=False)

I also have tried to use np.savetext and using with open(csvfile, "w") as output: without much success.

What I am getting is this

0   1   2   3   4   5   6   7   8   9
0   1   2   3   4                   

When i wish it look like this

0   0
1   1
2   2
3   3
4   4
5   
6   
7   
8   
9   

At the place of the append, I am thinking if it is possible to save everytime the array by specifying the column where it should go.

1
  • That csv layout will be hard to read. You could just iterate in python and write the file line by line Commented Sep 6, 2019 at 14:00

3 Answers 3

2

Although I'm unsure to why you're forming the df in this way.

use:

my_df = pd.DataFrame(list_of_thing)
my_df = my_df.T
my_df.to_csv(csvfile, index=False, header=False)
Sign up to request clarification or add additional context in comments.

1 Comment

it worked perfectly. Thanks a lot. I was not aware we could also use the transform on dataframe directly
0

Here a different method to input different sized lists to columns, i filled the smaller list with the character "":

import pandas as pd

a = np.arange(10)
b = np.arange(5)

maxLen = max(len(a), len(b))

fill_char = ""
a = list(a) + [fill_char]*(maxLen - len(a))
b = list(b) + [fill_char]*(maxLen - len(b))

my_df = pd.DataFrame(list(zip(a,b)),columns=["col1","col2"])

print(my_df)

   col1 col2
0     0    0
1     1    1
2     2    2
3     3    3
4     4    4
5     5     
6     6     
7     7     
8     8     
9     9     

Comments

0

You could use zip_longest to 'transpose' the list of arrays with a blank fill value:

In [120]: from itertools import zip_longest

In [129]: alist = list(zip_longest(np.arange(10), np.arange(5),fillvalue=''))                                
In [130]: alist                                                                                              
Out[130]: 
[(0, 0),
 (1, 1),
 (2, 2),
 (3, 3),
 (4, 4),
 (5, ''),
 (6, ''),
 (7, ''),
 (8, ''),
 (9, '')]

(default fillvalue is None)

Then just write this to a file, tuple by tuple:

In [131]: with open('foobar.txt','w') as f: 
     ...:     for row in alist: 
     ...:         print('{} {}'.format(*row), file=f) 
     ...:                                                                                                    
In [132]: cat foobar.txt                                                                                     
0 0
1 1
2 2
3 3
4 4
5 
6 
7 
8 
9 

This can also be written with savetxt:

np.savetxt('foobar.txt',alist, fmt='%s') 

With the string element, I have to use '%s'formatting rather than a numeric one.savetxt` does a row iteration, so isn't any faster.

You may have to play with column width and delimiter, especially if short lists are on the left.

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.