Home > Enterprise >  Pandas zfill multiple items in single cell
Pandas zfill multiple items in single cell

Time:12-01

I have multiple values in a single cell

Q3
1 4
1 3
3 4 11 
3 4 6 15 16

How can I zfill or pad to add leading zeros to each value in each cell?

df['Q3'].str.split(' ').apply(lambda x: x.zfill(8))

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

looking for

Q3
00000001 00000004
00000001 00000003
00000003 00000004 00000011 
00000003 00000004 00000006 00000015 00000016

CodePudding user response:

Simple. Split the values then apply zfill on each value and join back

df['Q3'].map(lambda x: ' '.join(y.zfill(8) for y in x.split()))

0                               00000001 00000004
1                               00000001 00000003
2                      00000003 00000004 00000011
3    00000003 00000004 00000006 00000015 00000016
Name: Q3, dtype: object
  • Related