-3

For Example:

col1
'ACDE'
'ADEC'

I have:

A = 0.89
C = 1.21
D = 1.33
E = 1.47

So the column should become

col1
[0.89, 1.21, 1.33, 1.47]
['0.89','1.33','1.47','1.21']
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Isaoud
  • 1
  • 3
  • Please read [ask] and explain the context of the problem. I **guess** you are using Pandas, so I added that tag. If so, you should at least explain this much. Ideally, show the starting values for the code as code, rather than as a table (for example, show code that will create that Dataframe). If this is **not** Pandas, then please explain what it is instead. – Karl Knechtel Sep 29 '22 at 02:45

1 Answers1

0

You should really not try to access variables by a string representation of their name.

Use a dictionary as container:

d = {'A': 0.89, 'C': 1.21, 'D': 1.33, 'E': 1.47}

# or
d = {'A': A, 'C': C, 'D': D, 'E': E}

Then you can break your string into characters and map the values:

df['col1'] = df['col1'].str.extractall('(.)')[0].map(d).groupby(level=0).agg(list)

Or using a list comprehension:

df['col1'] = [[d.get(c) for c in s] for s in df['col1']]

output:

                       col1
0  [0.89, 1.21, 1.33, 1.47]
1  [0.89, 1.33, 1.47, 1.21]
mozway
  • 194,879
  • 13
  • 39
  • 75