2

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]
1
  • Your data is list, not string: df.applymap(lamba x: ','.join(map(str,x))) Commented Oct 2, 2021 at 8:37

2 Answers 2

2

You should check out pandas.explode:

df.explode(['Distance', 'TGR Grade', 'TGR1'])
Sign up to request clarification or add additional context in comments.

Comments

1

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          

                                      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.