Python converting a dataframe of string type list to list

Viewed 66

I have a unique situation. I get a data from the field in row format. Each row contains a timestamp, a list of values. It is in string format. I am trying to convert it to a normal list.

My code:

df=
     A
0    '1.2,1.3'
1    '2.2,2.3'   
2    '3.2,3.3,'
3    '4.2,4.3'
import ast
df['A'] = df['A'].applymap(ast.literal_eval).applymap(list)

Present output:

ValueError: malformed node or string: 
[3.2,3.3]

Expected output:

df=
     A
0    [1.2,1.3]
1    [2.2,2.3]   
2    [3.2,3.3]
3    [4.2,4.3]
3 Answers

Try:

import ast

df["A"] = df["A"].apply(lambda x: ast.literal_eval("[" + x + "]"))
print(df)

Prints:

            A
0  [1.2, 1.3]
1  [2.2, 2.3]
2  [3.2, 3.3]
3  [4.2, 4.3]

If there are ' in the strings:

df["A"] = (
    df["A"].str.strip("'").apply(lambda x: ast.literal_eval("[" + x + "]"))
)

Try :

df["A"].str.replace("'", "").str.rstrip(",").str.split(",").apply(lambda x : list(map(lambda y : float(y), x)))

This cleans up the string for trailing commas and casts the values to floats.

One option can be to use pandas.str.extractall combined with pd.to_numeric:

df.apply(
     lambda s: s.str.extractall(r'(\d+(?:\.\d+))').values.ravel(),axis=1)
  .apply(pd.Series)
  .apply(pd.to_numeric)

     0    1
0  1.2  1.3
1  2.2  2.3
2  3.2  3.3
3  4.2  4.3
Related