The easiest way to print two numpy arrays as a table [closed]












-1














I have



x = np.array([1, 4, 5, ...100 more values])

y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values])


I need to output it as a table:



1   1.235
4 6.234
5 11.135


I need to print arrays vertically, justified to the right side and values should be rounded to 3 decimals. Is there any elegant way?



PS. I've tried this:



for i in range(n):
print(repr(x.item(i)).rjust((7)), repr(y.item(i)).rjust(7), end=' ')
print(repr(z.item(i)).rjust(7))


but it is not working.










share|improve this question















closed as off-topic by roganjosh, pushkin, petezurich, Patrick Mevzek, Max Vollmer Nov 17 '18 at 3:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – pushkin, Patrick Mevzek, Max Vollmer

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    Have you tried to implement any way from your research? What went wrong?
    – roganjosh
    Nov 16 '18 at 16:29






  • 2




    Do post the non-elegant ways you tried!!
    – Rahul Agarwal
    Nov 16 '18 at 16:30
















-1














I have



x = np.array([1, 4, 5, ...100 more values])

y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values])


I need to output it as a table:



1   1.235
4 6.234
5 11.135


I need to print arrays vertically, justified to the right side and values should be rounded to 3 decimals. Is there any elegant way?



PS. I've tried this:



for i in range(n):
print(repr(x.item(i)).rjust((7)), repr(y.item(i)).rjust(7), end=' ')
print(repr(z.item(i)).rjust(7))


but it is not working.










share|improve this question















closed as off-topic by roganjosh, pushkin, petezurich, Patrick Mevzek, Max Vollmer Nov 17 '18 at 3:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – pushkin, Patrick Mevzek, Max Vollmer

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    Have you tried to implement any way from your research? What went wrong?
    – roganjosh
    Nov 16 '18 at 16:29






  • 2




    Do post the non-elegant ways you tried!!
    – Rahul Agarwal
    Nov 16 '18 at 16:30














-1












-1








-1







I have



x = np.array([1, 4, 5, ...100 more values])

y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values])


I need to output it as a table:



1   1.235
4 6.234
5 11.135


I need to print arrays vertically, justified to the right side and values should be rounded to 3 decimals. Is there any elegant way?



PS. I've tried this:



for i in range(n):
print(repr(x.item(i)).rjust((7)), repr(y.item(i)).rjust(7), end=' ')
print(repr(z.item(i)).rjust(7))


but it is not working.










share|improve this question















I have



x = np.array([1, 4, 5, ...100 more values])

y = np.array([1.23452324, 6.2341238, 11.1348219, ...100 more values])


I need to output it as a table:



1   1.235
4 6.234
5 11.135


I need to print arrays vertically, justified to the right side and values should be rounded to 3 decimals. Is there any elegant way?



PS. I've tried this:



for i in range(n):
print(repr(x.item(i)).rjust((7)), repr(y.item(i)).rjust(7), end=' ')
print(repr(z.item(i)).rjust(7))


but it is not working.







python numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 16:36

























asked Nov 16 '18 at 16:27









George Zorikov

205




205




closed as off-topic by roganjosh, pushkin, petezurich, Patrick Mevzek, Max Vollmer Nov 17 '18 at 3:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – pushkin, Patrick Mevzek, Max Vollmer

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by roganjosh, pushkin, petezurich, Patrick Mevzek, Max Vollmer Nov 17 '18 at 3:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – pushkin, Patrick Mevzek, Max Vollmer

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    Have you tried to implement any way from your research? What went wrong?
    – roganjosh
    Nov 16 '18 at 16:29






  • 2




    Do post the non-elegant ways you tried!!
    – Rahul Agarwal
    Nov 16 '18 at 16:30














  • 3




    Have you tried to implement any way from your research? What went wrong?
    – roganjosh
    Nov 16 '18 at 16:29






  • 2




    Do post the non-elegant ways you tried!!
    – Rahul Agarwal
    Nov 16 '18 at 16:30








3




3




Have you tried to implement any way from your research? What went wrong?
– roganjosh
Nov 16 '18 at 16:29




Have you tried to implement any way from your research? What went wrong?
– roganjosh
Nov 16 '18 at 16:29




2




2




Do post the non-elegant ways you tried!!
– Rahul Agarwal
Nov 16 '18 at 16:30




Do post the non-elegant ways you tried!!
– Rahul Agarwal
Nov 16 '18 at 16:30












3 Answers
3






active

oldest

votes


















2














You can use pandas for this:



>>> import pandas
>>> import numpy
>>> x = numpy.array([1, 4, 5])
>>> y = numpy.array([1.23452324,6.2341238, 11.1348219])
>>> df = pandas.DataFrame({"x": x, "y": y})
>>> print(df)
x y
0 1 1.234523
1 4 6.234124
2 5 11.134822


Look up the Pandas documentation on how to format the numbers as you wish. In specific look for pandas.DataFrame.round:



>>> df.round(3)
x y
0 1 1.235
1 4 6.234
2 5 11.135


Make sure you installed Pandas on your machine:



Python2/Python3:



pip3 install --user pandas
pip install --user pandas





share|improve this answer























  • Wow! That's really what I was talking about. Thank you!
    – George Zorikov
    Nov 16 '18 at 16:55






  • 1




    No problem. I updated my answer to include the rounding.
    – Rafael
    Nov 16 '18 at 17:02



















2














>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
print('{0}t{1:.1f}'.format(x, y))

1 12.3
2 323.1
3 125.4
4 342.1
5 654.4
6 234.3





share|improve this answer























  • Almost what I want! If only you could tell me how to round these values I would be really thankful
    – George Zorikov
    Nov 16 '18 at 16:50












  • @GeorgeZorikov updated with rounding
    – Kurtis Streutker
    Nov 16 '18 at 17:00










  • Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
    – George Zorikov
    Nov 16 '18 at 17:09








  • 2




    to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
    – NaN
    Nov 17 '18 at 3:21



















1














Before you begin, install tabulate package



from terminal:



conda install tabulate


then the rest will be:



import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│ x │ y │
╞════════╪════════╡
│ 1.00 │ 1.23 │
├────────┼────────┤
│ 4.00 │ 6.23 │
├────────┼────────┤
│ 5.00 │ 11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛





share|improve this answer

















  • 1




    Also works fine. Thank you for answering my question!
    – George Zorikov
    Nov 16 '18 at 17:25


















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use pandas for this:



>>> import pandas
>>> import numpy
>>> x = numpy.array([1, 4, 5])
>>> y = numpy.array([1.23452324,6.2341238, 11.1348219])
>>> df = pandas.DataFrame({"x": x, "y": y})
>>> print(df)
x y
0 1 1.234523
1 4 6.234124
2 5 11.134822


Look up the Pandas documentation on how to format the numbers as you wish. In specific look for pandas.DataFrame.round:



>>> df.round(3)
x y
0 1 1.235
1 4 6.234
2 5 11.135


Make sure you installed Pandas on your machine:



Python2/Python3:



pip3 install --user pandas
pip install --user pandas





share|improve this answer























  • Wow! That's really what I was talking about. Thank you!
    – George Zorikov
    Nov 16 '18 at 16:55






  • 1




    No problem. I updated my answer to include the rounding.
    – Rafael
    Nov 16 '18 at 17:02
















2














You can use pandas for this:



>>> import pandas
>>> import numpy
>>> x = numpy.array([1, 4, 5])
>>> y = numpy.array([1.23452324,6.2341238, 11.1348219])
>>> df = pandas.DataFrame({"x": x, "y": y})
>>> print(df)
x y
0 1 1.234523
1 4 6.234124
2 5 11.134822


Look up the Pandas documentation on how to format the numbers as you wish. In specific look for pandas.DataFrame.round:



>>> df.round(3)
x y
0 1 1.235
1 4 6.234
2 5 11.135


Make sure you installed Pandas on your machine:



Python2/Python3:



pip3 install --user pandas
pip install --user pandas





share|improve this answer























  • Wow! That's really what I was talking about. Thank you!
    – George Zorikov
    Nov 16 '18 at 16:55






  • 1




    No problem. I updated my answer to include the rounding.
    – Rafael
    Nov 16 '18 at 17:02














2












2








2






You can use pandas for this:



>>> import pandas
>>> import numpy
>>> x = numpy.array([1, 4, 5])
>>> y = numpy.array([1.23452324,6.2341238, 11.1348219])
>>> df = pandas.DataFrame({"x": x, "y": y})
>>> print(df)
x y
0 1 1.234523
1 4 6.234124
2 5 11.134822


Look up the Pandas documentation on how to format the numbers as you wish. In specific look for pandas.DataFrame.round:



>>> df.round(3)
x y
0 1 1.235
1 4 6.234
2 5 11.135


Make sure you installed Pandas on your machine:



Python2/Python3:



pip3 install --user pandas
pip install --user pandas





share|improve this answer














You can use pandas for this:



>>> import pandas
>>> import numpy
>>> x = numpy.array([1, 4, 5])
>>> y = numpy.array([1.23452324,6.2341238, 11.1348219])
>>> df = pandas.DataFrame({"x": x, "y": y})
>>> print(df)
x y
0 1 1.234523
1 4 6.234124
2 5 11.134822


Look up the Pandas documentation on how to format the numbers as you wish. In specific look for pandas.DataFrame.round:



>>> df.round(3)
x y
0 1 1.235
1 4 6.234
2 5 11.135


Make sure you installed Pandas on your machine:



Python2/Python3:



pip3 install --user pandas
pip install --user pandas






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 17:04

























answered Nov 16 '18 at 16:32









Rafael

2,81932030




2,81932030












  • Wow! That's really what I was talking about. Thank you!
    – George Zorikov
    Nov 16 '18 at 16:55






  • 1




    No problem. I updated my answer to include the rounding.
    – Rafael
    Nov 16 '18 at 17:02


















  • Wow! That's really what I was talking about. Thank you!
    – George Zorikov
    Nov 16 '18 at 16:55






  • 1




    No problem. I updated my answer to include the rounding.
    – Rafael
    Nov 16 '18 at 17:02
















Wow! That's really what I was talking about. Thank you!
– George Zorikov
Nov 16 '18 at 16:55




Wow! That's really what I was talking about. Thank you!
– George Zorikov
Nov 16 '18 at 16:55




1




1




No problem. I updated my answer to include the rounding.
– Rafael
Nov 16 '18 at 17:02




No problem. I updated my answer to include the rounding.
– Rafael
Nov 16 '18 at 17:02













2














>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
print('{0}t{1:.1f}'.format(x, y))

1 12.3
2 323.1
3 125.4
4 342.1
5 654.4
6 234.3





share|improve this answer























  • Almost what I want! If only you could tell me how to round these values I would be really thankful
    – George Zorikov
    Nov 16 '18 at 16:50












  • @GeorgeZorikov updated with rounding
    – Kurtis Streutker
    Nov 16 '18 at 17:00










  • Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
    – George Zorikov
    Nov 16 '18 at 17:09








  • 2




    to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
    – NaN
    Nov 17 '18 at 3:21
















2














>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
print('{0}t{1:.1f}'.format(x, y))

1 12.3
2 323.1
3 125.4
4 342.1
5 654.4
6 234.3





share|improve this answer























  • Almost what I want! If only you could tell me how to round these values I would be really thankful
    – George Zorikov
    Nov 16 '18 at 16:50












  • @GeorgeZorikov updated with rounding
    – Kurtis Streutker
    Nov 16 '18 at 17:00










  • Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
    – George Zorikov
    Nov 16 '18 at 17:09








  • 2




    to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
    – NaN
    Nov 17 '18 at 3:21














2












2








2






>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
print('{0}t{1:.1f}'.format(x, y))

1 12.3
2 323.1
3 125.4
4 342.1
5 654.4
6 234.3





share|improve this answer














>>>import numpy as np 

>>>x = np.array([1, 2, 3, 4, 5, 6])
>>>y = np.array([12.3424, 323.1234, 125.4342, 342.1234, 654.4342, 234.3434])

>>>for x, y in zip(x, y):
print('{0}t{1:.1f}'.format(x, y))

1 12.3
2 323.1
3 125.4
4 342.1
5 654.4
6 234.3






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 16:59

























answered Nov 16 '18 at 16:37









Kurtis Streutker

48028




48028












  • Almost what I want! If only you could tell me how to round these values I would be really thankful
    – George Zorikov
    Nov 16 '18 at 16:50












  • @GeorgeZorikov updated with rounding
    – Kurtis Streutker
    Nov 16 '18 at 17:00










  • Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
    – George Zorikov
    Nov 16 '18 at 17:09








  • 2




    to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
    – NaN
    Nov 17 '18 at 3:21


















  • Almost what I want! If only you could tell me how to round these values I would be really thankful
    – George Zorikov
    Nov 16 '18 at 16:50












  • @GeorgeZorikov updated with rounding
    – Kurtis Streutker
    Nov 16 '18 at 17:00










  • Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
    – George Zorikov
    Nov 16 '18 at 17:09








  • 2




    to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
    – NaN
    Nov 17 '18 at 3:21
















Almost what I want! If only you could tell me how to round these values I would be really thankful
– George Zorikov
Nov 16 '18 at 16:50






Almost what I want! If only you could tell me how to round these values I would be really thankful
– George Zorikov
Nov 16 '18 at 16:50














@GeorgeZorikov updated with rounding
– Kurtis Streutker
Nov 16 '18 at 17:00




@GeorgeZorikov updated with rounding
– Kurtis Streutker
Nov 16 '18 at 17:00












Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
– George Zorikov
Nov 16 '18 at 17:09






Now there's a little bit strange output justification. I think pandas is a perfect fit in my case. Thank you anyway!
– George Zorikov
Nov 16 '18 at 17:09






2




2




to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
– NaN
Nov 17 '18 at 3:21




to play with the comma alignment and other formatting options... try.... for xy in list(zip(x, y)): print('{0:>3.0f} {1:>6.1f}'.format(*xy))
– NaN
Nov 17 '18 at 3:21











1














Before you begin, install tabulate package



from terminal:



conda install tabulate


then the rest will be:



import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│ x │ y │
╞════════╪════════╡
│ 1.00 │ 1.23 │
├────────┼────────┤
│ 4.00 │ 6.23 │
├────────┼────────┤
│ 5.00 │ 11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛





share|improve this answer

















  • 1




    Also works fine. Thank you for answering my question!
    – George Zorikov
    Nov 16 '18 at 17:25
















1














Before you begin, install tabulate package



from terminal:



conda install tabulate


then the rest will be:



import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│ x │ y │
╞════════╪════════╡
│ 1.00 │ 1.23 │
├────────┼────────┤
│ 4.00 │ 6.23 │
├────────┼────────┤
│ 5.00 │ 11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛





share|improve this answer

















  • 1




    Also works fine. Thank you for answering my question!
    – George Zorikov
    Nov 16 '18 at 17:25














1












1








1






Before you begin, install tabulate package



from terminal:



conda install tabulate


then the rest will be:



import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│ x │ y │
╞════════╪════════╡
│ 1.00 │ 1.23 │
├────────┼────────┤
│ 4.00 │ 6.23 │
├────────┼────────┤
│ 5.00 │ 11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛





share|improve this answer












Before you begin, install tabulate package



from terminal:



conda install tabulate


then the rest will be:



import numpy as np
from tabulate import tabulate


x = np.array([1, 4, 5, 100])
y = np.array([1.23452324, 6.2341238, 11.1348219, 100])

col_headers = ["x", "y"]

merged_array = np.array([x, y]).T


table = tabulate(merged_array , col_headers, tablefmt="fancy_grid", floatfmt = ".2f")

print(table)
╒════════╤════════╕
│ x │ y │
╞════════╪════════╡
│ 1.00 │ 1.23 │
├────────┼────────┤
│ 4.00 │ 6.23 │
├────────┼────────┤
│ 5.00 │ 11.13 │
├────────┼────────┤
│ 100.00 │ 100.00 │
╘════════╧════════╛






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 17:15









Ken Dekalb

321111




321111








  • 1




    Also works fine. Thank you for answering my question!
    – George Zorikov
    Nov 16 '18 at 17:25














  • 1




    Also works fine. Thank you for answering my question!
    – George Zorikov
    Nov 16 '18 at 17:25








1




1




Also works fine. Thank you for answering my question!
– George Zorikov
Nov 16 '18 at 17:25




Also works fine. Thank you for answering my question!
– George Zorikov
Nov 16 '18 at 17:25



Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents