0

I want to print all number between two numbers

df =             code1           code2
                 34261           [54642-54646]
                 [32413-32417]   [23541-23546, 42156-42158]

Based on this dataframe I want following output dataframe

df =             code1                                  code2
                 34261                                  [54642,54642, 54644, 54645, 54646]
                 [32413,32414, 32415,32416, 32417]      [23541,23542, 23543, 23544, 23545, 23546, 42156,42157,42158]
0

1 Answer 1

0

There is combination list with scalars numbers, so linked solution was modified:

#https://stackoverflow.com/a/6405228/2901002
def f(x):
    if isinstance(x, list):
        result = []
        for y in x:
            for part in y.split(','):
                if '-' in part:
                    a, b = part.split('-')
                    a, b = int(a), int(b)
                    result.extend(range(a, b + 1))
                else:
                    a = int(part)
                    result.append(a)
        else: 
        result = x
    
    return result


cols = ['code1','code2']
df[cols] = df[cols].applymap(f)
print (df)
                                 code1  \
0                                34261   
1  [32413, 32414, 32415, 32416, 32417]   

                                               code2  
0                [54642, 54643, 54644, 54645, 54646]  
1  [23541, 23542, 23543, 23544, 23545, 23546, 421...  
Sign up to request clarification or add additional context in comments.

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.