Python Forum
How to concatenate elements of different type in a table cell? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to concatenate elements of different type in a table cell? (/thread-3898.html)



How to concatenate elements of different type in a table cell? - Felipe - Jul-06-2017

Hi guys,

I wrote a code to print some tables, and to do that I used the following lines of code to fill my cells with the values of my array p:
for row in range(prism):
   cell_text.append(['%d' % item for item in p[row,0:4]])
That works ok. But now I have a list data_prec of values which represents the precision of the values of the array p.
So, in the position 0,0 from data_prec I have the precision for the value of p on the position 0,0. I mean, for the value 1 in the array p, the precision is 0.01 as you can see in the following example:
Output:
Array p: [1, 2, 3, 4] List data_prec: [array([0.01, 0.02, 0.05, 0.02])]
So I need to fill the cells of my tables with an element of p and its correspondent precision, I mean something like 1 +- 0.01.
I tried to modify the code above but doesn't worked. Here's my attempt:
for row in range(prism):
   cell_text.append(['%d +- %d' % item for item in p[row,0:4], % elem for elem in data_prec[row,0:4]])
I appreciate if anyone can help me. Thanks !!


RE: How to concatenate elements of different type in a table cell? - Larz60+ - Jul-06-2017

please provide some raw data before the slice


RE: How to concatenate elements of different type in a table cell? - Felipe - Jul-06-2017

(Jul-06-2017, 03:03 PM)Larz60+ Wrote: please provide some raw data before the slice

Here's my array p:
[[2.07155254 -20.60156854  19.5483871    4.19432936]
[1.76661686 -32.39288267  30.23363804   3.77398568]
[2.00349085  24.80629016  49.63222236   6.13728671]
[1.58312452 -35.20497126  60.14467708   3.56179955]
[2.10054201  -2.98785155  71.37096774   6.75637713]]
And my list data_prec:
[array([ 0.00843336,  0.47147996,  0.03038715,  0.01146302]), 
array([ 0.007655  ,  0.15879539,  0.        ,  0.00602976]), 
array([ 0.01001745,  0.1715518 ,  0.02186042,  0.01043048]), 
array([ 0.00677647,  0.01066192,  0.        ,  0.01380245]), 
array([ 0.        ,  0.18368726,  0.        ,  0.00046686])]
prism = 5


RE: How to concatenate elements of different type in a table cell? - Larz60+ - Jul-07-2017

I'm still not sure what you're trying to do, but to use a 'list' in python
(which is an array), and split off the mantissa and exponent of a float,
you can use something like:
p = [2.07155254, -20.60156854, 19.5483871, 4.19432936]
# Split mantissa and exponent
for n, value in enumerate(p):
   parts = str(value).split('.')
   print('Value: {}, mantissa: {}, exponent: {}'.format(n, parts[0], parts[1]))
which will produce:
Output:
Value: 0, mantissa: 2, exponent: 07155254 Value: 1, mantissa: -20, exponent: 60156854 Value: 2, mantissa: 19, exponent: 5483871 Value: 3, mantissa: 4, exponent: 19432936



RE: How to concatenate elements of different type in a table cell? - Felipe - Jul-07-2017

(Jul-07-2017, 12:05 AM)Larz60+ Wrote: I'm still not sure what you're trying to do, but to use a 'list' in python
(which is an array), and split off the mantissa and exponent of a float,
you can use something like:
p = [2.07155254, -20.60156854, 19.5483871, 4.19432936]
# Split mantissa and exponent
for n, value in enumerate(p):
   parts = str(value).split('.')
   print('Value: {}, mantissa: {}, exponent: {}'.format(n, parts[0], parts[1]))
which will produce:
Output:
Value: 0, mantissa: 2, exponent: 07155254 Value: 1, mantissa: -20, exponent: 60156854 Value: 2, mantissa: 19, exponent: 5483871 Value: 3, mantissa: 4, exponent: 19432936

Let me try to explain. The array "p" has values of parameters calculated by a program and the list "data_prec" has the standard deviation for every number of "p".
So, I need to construct a table, in which every cell contains a value from "p" and its respective standard deviation from "data_prec". To do that, the "cell_text" needs to receive something like 2.07155254 ± 0.00843336, because I use "cell_text" to fill my table. I mean, if I can pair up every value of "p" with a value of "data_prec", and put it in "cell_text", I can use it to plot my table.

To plot my table without the values of standard deviation(data_prec), I use the following code:
rows = ['%d' % i for i in np.arange(1,prism+1)]
   columns = ('Mt (A)','inc ($^\circ$)','x0 (m)','z0 (m)')

   cell_text = []
   for row in range(prism):
       cell_text.append(['%d' % item for item in p[row,0:4]])

   ax6=plt.subplot2grid((3,3), (2,2), colspan=1)
   ax6.table(cellText=cell_text,rowLabels=rows,colLabels=columns,loc='center')
   ax6.get_xaxis().set_visible(False)
   ax6.get_yaxis().set_visible(False)
That's the reason why I tried to use this line in my first post, but without success:
cell_text.append(['%d +- %d' % item for item in p[row,0:4], % elem for elem in data_prec[row,0:4]])



RE: How to concatenate elements of different type in a table cell? - Larz60+ - Jul-07-2017

question 1
Ok, so the p array is actually a text buffer that contains values of an array, but not a syntactically correct python structure.
is that correct?

question 2
and you need to parse those values to match up with the data_prec which is a syntactically correct
python list containing your standard deviation
is that correct?

question 3
With this assumption, if the p array is converted to a syntactically correct python list you'll be in a position to
take control and do the matching.
Is that correct?


RE: How to concatenate elements of different type in a table cell? - Felipe - Jul-07-2017

(Jul-07-2017, 03:19 AM)Larz60+ Wrote: question 1
Ok, so the p array is actually a text buffer that contains values of an array, but not a syntactically correct python structure.
is that correct?

question 2
and you need to parse those values to match up with the data_prec which is a syntactically correct
python list containing your standard deviation
is that correct?

question 3
With this assumption, if the p array is converted to a syntactically correct python list you'll be in a position to
take control and do the matching.
Is that correct?

Question 1
I used numpy array to construct the array "p", for storing the values of a matrix 4x5. How I'm a beginner in python, I'm not so sure if theres a syntax error here... The "list data_prec" has data of a 4x5 matrix stored too.

Question 2
Yes, it's correct.

Question 3
I believe that it's correct. If I have the same type of data to fill the "cell_text", would be easier to deal with it.


RE: How to concatenate elements of different type in a table cell? - Larz60+ - Jul-07-2017

You should be able to convert the numpy array to a list like:
import numpy as np

your_nump_array = [] # replace with your data
plist = np.array(your_nump_array).tolist()
print(plist)
I'm not a big user of numpy, so you may have to play with this a bit.


RE: How to concatenate elements of different type in a table cell? - Felipe - Jul-07-2017

(Jul-07-2017, 10:46 AM)Larz60+ Wrote: You should be able to convert the numpy array to a list like:
import numpy as np

your_nump_array = [] # replace with your data
plist = np.array(your_nump_array).tolist()
print(plist)
I'm not a big user of numpy, so you may have to play with this a bit.

Thank you !! I will try this option. If I have success, I'll post a feedback here.