Python Forum
How to concatenate elements of different type in a table cell?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to concatenate elements of different type in a table cell?
#1
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 !!
Reply
#2
please provide some raw data before the slice
Reply
#3
(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
Reply
#4
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
Reply
#5
(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]])
Reply
#6
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?
Reply
#7
(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.
Reply
#8
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.
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate 3D arrays paul18fr 1 2,589 Apr-09-2021, 02:49 PM
Last Post: paul18fr
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 2,702 May-08-2020, 01:41 PM
Last Post: nnk
  How to prepare a NumPy array which include float type array elements subhash 0 1,884 Mar-02-2020, 06:46 AM
Last Post: subhash
  concatenate mcgrim 1 2,199 Mar-22-2019, 01:31 PM
Last Post: buran
  Checking the elements of a matrix with an elements of a list juniorcoder 11 5,755 Sep-17-2018, 03:02 PM
Last Post: gruntfutuk
  Slicing String cell by cell Vigneshkumarsakthivel 0 2,358 Sep-02-2018, 05:59 PM
Last Post: Vigneshkumarsakthivel
  Concatenate Specific Cell Interval With Pandas vidividi12 0 2,818 Oct-01-2017, 02:16 PM
Last Post: vidividi12
  How can i convert a string to an array with elements type float 64 zoya2385 3 6,081 May-11-2017, 03:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020