Home > Software engineering >  How to extract values from a column of list string pandas dataframe
How to extract values from a column of list string pandas dataframe

Time:10-03

I want to extract the values from the list so that I can perform some pandas operation on the string.

Distance                           TGR Grade                  TGR1
[342m, 342m, 530m, 342m]         [M, M, RW, RW]            [1, 1, 7, 1]
[390m, 390m, 390m, 390m,450]    [M, 7, 6G, X45, X67]       [1, 2, 4, 5, 5]
[]                                    []                      []

I need a clean df of this form.

    Distance                     TGRGrade             TGR1
342m,342m,530m,342m          M,M,RW,RW            1,1,7,1
390m,390m,390m,390m,450      M,7,6G,X45,X67       1,2,4,5,5

I have tried the below functions:

df.columns = [''.join(i.split()) for i in df.columns]
df = df.applymap(lambda x: ''.join(x.strip('\[').strip('\]').split()))

and

df = df.replace('[', '')
df = df.replace(']', '')

My first attempt lead to this error.

AttributeError: 'list' object has no attribute 'strip'

checking the values in the individual column resulted in this.

df['TGR1']

    0           [1, 1, 7, 1, 1, 8, 8, 1, 1, 8]
    1           [1, 2, 4, 5, 5, 1, 2, 7, 6, 8]
    2     [6, 1, 4, 4, 7, 1, 7, 1, 8, 3, 4, 5]
    3     [1, 7, 4, 4, 3, 2, 1, 1, 2, 2, 2, 1]
    4     [3, 4, 5, 2, 1, 8, 5, 2, 3, 6, 5, 3]

CodePudding user response:

You should check out pandas.explode:

df.explode(['Distance', 'TGR Grade', 'TGR1'])

CodePudding user response:

Try:

df = df.apply(lambda x: x.apply(lambda x: ','.join(map(str, x))))

OUTPUT:

                  Distance       TGR Grade       TGR1
0      342m,342m,530m,342m       M,M,RW,RW    1,1,7,1
1  390m,390m,390m,390m,450  M,7,6G,X45,X67  1,2,4,5,5
2          

                                      
  • Related