0

I have a column in dataframe, which is created from the column name of another dataframe (with melt function. Therefore the values are strings and not lists with floats inside. Columns look like this:

interval
[100.00, 3.00]
[3.00, 2.00]
[2.00, 1.00]
[1, 0.25]
[0.25, 0.00]

What I need is a column with a list of floats. For loops doesn't look like a good or elegant idea and I don't have any other...

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Vesnič
  • 365
  • 5
  • 17
  • What does the output `df['interval'].dtype` yield? I suspect this will give you the object data type. If you were to drill down into the contents of interval, you will find the column is of data type object, but each row object is a list of floats. For example do something like `type(df.at[0,'interval'])` and `type(df.at[0,'interval'][0])` to examine the first row. – Jason Cook Sep 13 '21 at 13:42
  • It is an object, but the object is in fact string that could not be converted to floats. After trying hard for quite some time I realized, that each element is a string like "[100.00, 3.00]" – Vesnič Sep 13 '21 at 13:46

1 Answers1

3

Use ast.literal_eval:

import ast

df['interval'] = df['interval'].apply(ast.literal_eval)

Output

>>> df
       interval
0  [100.0, 3.0]
1    [3.0, 2.0]
2    [2.0, 1.0]
3     [1, 0.25]
4   [0.25, 0.0]

>>> df.loc[0, 'interval']
[100.0, 3.0]

>>> type(df.loc[0, 'interval'])
list

Now you can convert to columns if you want:

>>> df['interval'].apply(pd.Series)
        0     1
0  100.00  3.00
1    3.00  2.00
2    2.00  1.00
3    1.00  0.25
4    0.25  0.00
Corralien
  • 109,409
  • 8
  • 28
  • 52