0

I have a pandas dataframe with a integer column called TDLINX. I'm trying to convert that to a string with leading zeros such that all values are 7 characters, with leading zeros. So 7 would become "0000007"

This is the code that I used:

df_merged_total['TDLINX2'] = df.TDLINX.apply(lambda x: str(x).zfill(7))

At first glance this appeared to work, but as I went further down the file, I realized that the value in TDLINX2 was starting to get shifted. What could be causing this and what can I do to prevent it?

3
  • Please show a self-contained example demonstrating your problem. Commented Apr 15, 2016 at 20:25
  • 2
    if your indices are not aligned between df_merged_total and df then you will observe an offset what does df_merged_total.index.difference(df.index) show? Commented Apr 15, 2016 at 20:43
  • You're right. That's definitely the problem. Thanks! Commented Apr 15, 2016 at 20:58

1 Answer 1

2

You could do something like this:

>>> df = pd.DataFrame({"col":[1, 33, 555, 7777]})
>>> df["new_col"] = ["%07d" % x for x in df.col]

>>> df

    col  new_col
0     1  0000001
1    33  0000033
2   555  0000555
3  7777  0007777
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.