Pandas zfill multiple items in single cell

Question:

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
Asked By: pdangelo4

||

Answers:

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
Answered By: Shubham Sharma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.