Home > Enterprise >  How to convert list like value in each row into pure value in python dataframe?
How to convert list like value in each row into pure value in python dataframe?

Time:10-22

I have a dataframe that has a value that looks as belows

colA    desired
[0]       0
[3, 1]    3,1
[3, 1, 2] 3,1,2
[3, 1]    3,1

The type for colA is object. Is there a way to do it? Thanks

CodePudding user response:

without the lambda as that will be slower for larger data sets, you can simply cast the list to a string type then strip unwanted characters.

import pandas as pd
df = pd.DataFrame(data={'colA':[[0], [3,1],[3,1,2],[3,1]]})
df['desired'] = df.colA.astype(str).str.replace('\[|\]|\'', '')
df

Output:

colA            desired
0   [0]         0
1   [3, 1]      3, 1
2   [3, 1, 2]   3, 1, 2
3   [3, 1]      3, 1

CodePudding user response:

Try:

df = pd.DataFrame(data={'colA':[[0], [3,1],[3,1,2],[3,1], [4]]})
df['desired'] = df['colA'].apply(lambda x: ','.join(map(str, x)))

OUTPUT:

        colA desired
0        [0]       0
1     [3, 1]     3,1
2  [3, 1, 2]   3,1,2
3     [3, 1]     3,1
4        [4]       4

If colA is obj:

from ast import literal_eval
df = pd.DataFrame(data={'colA':["[0]", "[3,1]","[3,1,2]","[3,1]", "[4]"]})
df['desired'] = df['colA'].apply(lambda x: ','.join(map(str, literal_eval(x))))

CodePudding user response:

You can use str.replace:

df['desired'] = df['colA'].str.replace(r'[][]', '', regex=True)

Prints:

        colA  desired
0        [0]        0
1     [3, 1]     3, 1
2  [3, 1, 2]  3, 1, 2
3     [3, 1]     3, 1

You can use the regex demo to play with it.

  • Related