How to fill a str column with spaces to force a desired length?

Viewed 101

I need to prepare a pandas DataFrame to write a TXT for submission to a third party on a strict format. When I use the following code directly it seems to work:

data['code'].apply(lambda x: '{:>{fill}}'.format(x, fill=15))

Bu when I try to actually change the DataFrame like:

data['code'] = data['code'].apply(lambda x: '{:>{fill}}'.format(x, fill=15))

It doesn't seem to take. How do I actually change the DataFrame?

1 Answers

Check with ljust or zfill

data['code'] = data['code'].str.ljust(width = 15, fillchar = ':')
Related