0

I have column names in a dataframe

ID c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 ... cN

but I want it to look like

ID c01 c02 c03 c04 c05 c06 c07 c08 c09 c10 c11 c12 ... cN

How can I only change select column names without changing any others?

1
  • See this Commented Jun 18, 2018 at 18:32

1 Answer 1

3

You can use zfill to get the leading zeroes:

l=[]
for i in range(1,20):
    l.append('c'+str(i).zfill(2))
print(l)

['c01', 'c02', 'c03', 'c04', 'c05', 'c06', 'c07', 'c08', 'c09', 'c10', 'c11', 'c12', 'c13', 'c14', 'c15', 'c16', 'c17', 'c18', 'c19']

And then add 'ID' to this list and then assign it as df.columns.

Sign up to request clarification or add additional context in comments.

3 Comments

So there is no way to actually do it within the dataframe?
am afraid once you define the columns as alphanumeric (c1, c2, etc), you cannot insert a leading zero in between.
I want to use df.rename but I am not sure how to actually go into the specific names and change them.

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.