1

I have a data table. One of the cols is sample number and it's looks that:

0002131
dsda2123
eew12341
wewqsws1
123

As U can see the numbers have a different length. I need to reach all == 12. So if for example I have a 0002131 I just add 5 zeros in first positions : 000000002131. My series has 900000 rows, and I just I put it all to listA and do it by if/else. Code:

for i in range(len(listA)): 
    if len(listA[i]) == 0:
        print("COŚ JEST NIE TAK!")
    elif len(listA[i]) == 1:
        listA[i] = "00000000000"+listA[i]
    elif len(listA[i]) == 2:
        listA[i] = "0000000000"+listA[i]
    elif len(listA[i]) == 3:
        listA[i] = "000000000"+listA[i]
    elif len(listA[i]) == 4:
        listA[i] = "00000000"+listA[i]
    elif len(listA[i]) == 5:
        listA[i] = "0000000"+listA[i]
    elif len(listA[i]) == 6:
        listA[i] = "000000"+listA[i]
    elif len(listA[i]) == 7:
        listA[i] = "00000"+listA[i]
    elif len(listA[i]) == 8:
        listA[i] = "0000"+listA[i]
    elif len(listA[i]) == 9:
        listA[i] = "000"+listA[i]
    elif len(listA[i]) == 10:
        listA[i] = "00"+listA[i]
    elif len(listA[i]) == 11:
        listA[i] = "0"+listA[i]

It works but I know It's not a best method. So I tried it in pandas style like below:

test['SampleNumber'].where(len(test) < 1, '00000000000' + test['SampleNumber'])
test['SampleNumber'].where(len(test) < 2, '0000000000' + test['SampleNumber'])

But I get: Array conditional must be same shape as self.

Maybe someone have a better idea than my else if and can share it with me?

martin
  • 1,145
  • 1
  • 7
  • 24

0 Answers0