0

I start out with a pandas dataframe and a 1x8 numpy array of indices:

0  0,   0.0035
1  1,   0.0070
2  2,   0.0025
3  3,   0.0005
4  4,   0.0105
5  5,   0.0015
6  6,   0.0085
7  7,   0.0055
8  8,   0.0060
9  9,   0.0030

array([0, 2, 4, 8, 9, 5, 3, 1])

I'd like to return a mapping result with the array the "key":

array([0.0035, 0.0025, 0.0105, 0.0060, 0.0030, 0.0015, 0.0005, 0.0070])

I attempt

new_list = [ map(float, nodes2[i]['node,prob'].split(',')) for i in indices[0]]
                 
for item in new_list:
    print(item)

which returns below.

<map object at 0x000001B4898AFF88>
<map object at 0x000001B4898AF408>
<map object at 0x000001B489591C48>
<map object at 0x000001B48959D688>
<map object at 0x000001B4898CF608>
<map object at 0x000001B4898CF7C8>
<map object at 0x000001B489962908>

Now of course, rather than memory addresses (pointers to?), liked to see their content! I've seen the loop itemizing the list solve this in another Q&A here. Why isn't it working here? Thanks!

  • What do you want as the output (array or list or dictt)? Mention it clearly please. – Sai Sreenivas Jul 22 '20 at 14:56
  • Use `list(map(...))` to get a list of the results of the mapping. – Barmar Jul 22 '20 at 15:21
  • Or use `print(*item)` to print them as separate elements instead of printing the `map` object. – Barmar Jul 22 '20 at 15:21
  • No, `*` means to spread the elements of `item` into separate arguments. See https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – Barmar Jul 22 '20 at 19:47
  • It printed ```0.0 0.0035 2.0 0.0025 4.0 0.0105 8.0 0.006 9.0 0.003 5.0 0.0015 3.0 0.0005 1.0 0.007``` as expected. Now, how do I break up two columns of a map and assign to separate arrays is the remaining question. – Gui Larange Jul 22 '20 at 20:26

1 Answers1

0

Full answer is given by:

nodes = pd.read_table("Nodes10.csv", delimiter = " ")

nodes2 = nodes.to_dict("index")

new_list = [map(float, nodes2[i]['node,prob'].split(',')) for i in indices[0]]
          
l = len(new_list)
a = np.zeros(l)
i = 0
for item in new_list:
 
   for lm in item:
       a[i]=lm
   i = i+1