3

I'm trying to print the output of an astronomy simulation so that it looks nice in my console. I generate 4 numpy arrays called Amplitude, Mass, Period and Eccentricity and I want to put them in a table. The first index of each array are the values for planet 1, the second for planet 2 etc.

So my arrays look like (values are all float numbers, eg 'a1' just a placeholder):

amp = [a1 a2 a3 a4]
mass = [m1 m2 m3 m4]
period = [p1 p2 p3 p4]
ecc = [e1 e2 e3 e4]

I'd like my table to look like:

planet|amp|mass|period|ecc
1     |a1 |m1  |p1    |e1
2     |a2 |m2  |p2    |e2
...

I've tried using tabulate and something like:

print tabulate(['1', amp[0], mass[0], period[0], ecc[0]], headers=[...])

but I get an error of 'numpy.float64' object is not iterable

Any help would be appreciated!

4 Answers 4

7

You can combine the usage of zip() with tabulate to create a nicer looking table:

from tabulate import tabulate
headers = ['planet', 'amp', 'mass', 'period', 'ecc']    

amp = [1.1, 1.2, 1.3, 1.4]
mass = [2.1, 2.2, 2.3, 2.4]
period = [3.1, 3.2, 3.3, 3.4]
ecc = [4.1, 4.2, 4.3, 4.4]
planet = range(1, len(amp)+1)

table = zip(planet, amp, mass, period, ecc)
print(tabulate(table, headers=headers, floatfmt=".4f"))

Output:

  planet     amp    mass    period     ecc
--------  ------  ------  --------  ------
       1  1.1000  2.1000    3.1000  4.1000
       2  1.2000  2.2000    3.2000  4.2000
       3  1.3000  2.3000    3.3000  4.3000
       4  1.4000  2.4000    3.4000  4.4000
Sign up to request clarification or add additional context in comments.

Comments

4

Use zip() like this:

amp = ['a1', 'a2', 'a3', 'a4']
mass = ['m1', 'm2', 'm3', 'm4']
period = ['p1', 'p2', 'p3', 'p4']
ecc = ['e1', 'e2', 'e3', 'e4']
planet = [1, 2, 3, 4]


titles = ['planet', 'amp', 'mass', 'period', 'ecc']

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for item in zip(planet, amp, mass, period, ecc):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*item)

Instead of using planet = [1, 2, 3, 4], you can use enumerate() like below:

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for i, item in enumerate(zip(amp, mass, period, ecc)):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(i+1, *item)

Output:

>>> python print_table.py
planet|amp   |mass  |period|ecc
1     |a1    |m1    |p1    |e1
2     |a2    |m2    |p2    |e2
3     |a3    |m3    |p3    |e3
4     |a4    |m4    |p4    |e4

6 Comments

Thanks for the suggestion. This prints out the column titles in 'titles' but then nothing else underneath. I might have to redefine my arrays like you did...
@RichardHall You need to run the for loop as well. You can also put the above code in a file like I did with print_table.py
The enumerate worked! Thanks! Follow up question, can I format it so that each 'cell' of the table is the same length/size?
Sure, that's what I have done with :<6. I am using 6 because the longest word is 6 characters (planet and period)
Yeah I thought that was controlling it, but the values (eg 'a1') are showing with up 11 decimal places, but others are whole numbers so only show one decimal place ...
|
0

tabulate.tabulate() takes a list of lists (or arrays) as its main argument, you've given it a list of floats (and a '1').

So you want to put in all your data at once.

You'll need to create new lists where each is one horizontal row of the new table. You'll also need to add the row number at the front.

First build new lists/arrays where:

list1=[1,a1,m1,p1,e1]
list2=[2,a2,m2,p2,e2]
...

And then try:

print tabulate([list1, list2, list3, list4], headers=[...])

Comments

-3

You can use \t.

For example:

print 'a\t','b\t','\n','c\t','d\t'

output:

a   b   
c   d   

3 Comments

This does not answer the question and would be better suited as a comment. You will be able to comment once you have sufficient reputation.
@TigerhawkT3 No. This is an answer to the question.
@FelixSFD - No, it isn't an answer to the question. The question is already asking how to tabulate their arrays. This answer is more like a guess regarding the meaning of "tabulate." That doesn't provide an answer or help in any way.

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.