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]
Beatdown
  • 187
  • 2
  • 7
  • 20

1 Answers1

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...  
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252