Python pandas dataframe change content based on string length












2















I have a dataframe like below.



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])


so if you look at the row 3,4,5 the length of the ids are less than 6



id
111111
123456
12345
234
12
987654


I want to convert it to below(Basically append zeroes in the front to make it a length of six)



id
111111
123456
012345
000234
000012
987654









share|improve this question























  • Perhaps take a look at stackoverflow.com/questions/134934/…

    – sfjac
    Jan 2 at 2:41
















2















I have a dataframe like below.



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])


so if you look at the row 3,4,5 the length of the ids are less than 6



id
111111
123456
12345
234
12
987654


I want to convert it to below(Basically append zeroes in the front to make it a length of six)



id
111111
123456
012345
000234
000012
987654









share|improve this question























  • Perhaps take a look at stackoverflow.com/questions/134934/…

    – sfjac
    Jan 2 at 2:41














2












2








2








I have a dataframe like below.



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])


so if you look at the row 3,4,5 the length of the ids are less than 6



id
111111
123456
12345
234
12
987654


I want to convert it to below(Basically append zeroes in the front to make it a length of six)



id
111111
123456
012345
000234
000012
987654









share|improve this question














I have a dataframe like below.



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])


so if you look at the row 3,4,5 the length of the ids are less than 6



id
111111
123456
12345
234
12
987654


I want to convert it to below(Basically append zeroes in the front to make it a length of six)



id
111111
123456
012345
000234
000012
987654






python pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 2:38









Sandhya ThotakuraSandhya Thotakura

3369




3369













  • Perhaps take a look at stackoverflow.com/questions/134934/…

    – sfjac
    Jan 2 at 2:41



















  • Perhaps take a look at stackoverflow.com/questions/134934/…

    – sfjac
    Jan 2 at 2:41

















Perhaps take a look at stackoverflow.com/questions/134934/…

– sfjac
Jan 2 at 2:41





Perhaps take a look at stackoverflow.com/questions/134934/…

– sfjac
Jan 2 at 2:41












3 Answers
3






active

oldest

votes


















2














Using



df.id.astype(str).str.pad(6,'left','0')
0 111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    @ScottBoston done with that : -)

    – Wen-Ben
    Jan 2 at 2:47











  • Very kind of you.

    – Scott Boston
    Jan 2 at 2:48











  • @SandhyaThotakura yw :_ happy new year

    – Wen-Ben
    Jan 2 at 2:55



















3














Try zfill, first convert integers to string dtype then use string accessor then zfill:



df['id'].astype(str).str.zfill(6)


Output:



0    111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

    – Sandhya Thotakura
    Jan 2 at 2:53











  • @SandhyaThotakura Happy coding!

    – Scott Boston
    Jan 2 at 2:53



















1














You have to change the datatype to string or else the initial zeroes will be trimmed.
Below is my solution. Hope it helps



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])
df = df.astype(str)
df['id'] = df['id'].apply(lambda x: x if len(x)==6 else '0'*(6-len(x))+x)

df:
id
111111
123456
012345
000234
000012
987654





share|improve this answer
























  • Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

    – Sandhya Thotakura
    Jan 2 at 2:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000618%2fpython-pandas-dataframe-change-content-based-on-string-length%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Using



df.id.astype(str).str.pad(6,'left','0')
0 111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    @ScottBoston done with that : -)

    – Wen-Ben
    Jan 2 at 2:47











  • Very kind of you.

    – Scott Boston
    Jan 2 at 2:48











  • @SandhyaThotakura yw :_ happy new year

    – Wen-Ben
    Jan 2 at 2:55
















2














Using



df.id.astype(str).str.pad(6,'left','0')
0 111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    @ScottBoston done with that : -)

    – Wen-Ben
    Jan 2 at 2:47











  • Very kind of you.

    – Scott Boston
    Jan 2 at 2:48











  • @SandhyaThotakura yw :_ happy new year

    – Wen-Ben
    Jan 2 at 2:55














2












2








2







Using



df.id.astype(str).str.pad(6,'left','0')
0 111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer













Using



df.id.astype(str).str.pad(6,'left','0')
0 111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 2:45









Wen-BenWen-Ben

118k83469




118k83469








  • 1





    @ScottBoston done with that : -)

    – Wen-Ben
    Jan 2 at 2:47











  • Very kind of you.

    – Scott Boston
    Jan 2 at 2:48











  • @SandhyaThotakura yw :_ happy new year

    – Wen-Ben
    Jan 2 at 2:55














  • 1





    @ScottBoston done with that : -)

    – Wen-Ben
    Jan 2 at 2:47











  • Very kind of you.

    – Scott Boston
    Jan 2 at 2:48











  • @SandhyaThotakura yw :_ happy new year

    – Wen-Ben
    Jan 2 at 2:55








1




1





@ScottBoston done with that : -)

– Wen-Ben
Jan 2 at 2:47





@ScottBoston done with that : -)

– Wen-Ben
Jan 2 at 2:47













Very kind of you.

– Scott Boston
Jan 2 at 2:48





Very kind of you.

– Scott Boston
Jan 2 at 2:48













@SandhyaThotakura yw :_ happy new year

– Wen-Ben
Jan 2 at 2:55





@SandhyaThotakura yw :_ happy new year

– Wen-Ben
Jan 2 at 2:55













3














Try zfill, first convert integers to string dtype then use string accessor then zfill:



df['id'].astype(str).str.zfill(6)


Output:



0    111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

    – Sandhya Thotakura
    Jan 2 at 2:53











  • @SandhyaThotakura Happy coding!

    – Scott Boston
    Jan 2 at 2:53
















3














Try zfill, first convert integers to string dtype then use string accessor then zfill:



df['id'].astype(str).str.zfill(6)


Output:



0    111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer



















  • 1





    Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

    – Sandhya Thotakura
    Jan 2 at 2:53











  • @SandhyaThotakura Happy coding!

    – Scott Boston
    Jan 2 at 2:53














3












3








3







Try zfill, first convert integers to string dtype then use string accessor then zfill:



df['id'].astype(str).str.zfill(6)


Output:



0    111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object





share|improve this answer













Try zfill, first convert integers to string dtype then use string accessor then zfill:



df['id'].astype(str).str.zfill(6)


Output:



0    111111
1 123456
2 012345
3 000234
4 000012
5 987654
Name: id, dtype: object






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 2:45









Scott BostonScott Boston

56.7k73158




56.7k73158








  • 1





    Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

    – Sandhya Thotakura
    Jan 2 at 2:53











  • @SandhyaThotakura Happy coding!

    – Scott Boston
    Jan 2 at 2:53














  • 1





    Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

    – Sandhya Thotakura
    Jan 2 at 2:53











  • @SandhyaThotakura Happy coding!

    – Scott Boston
    Jan 2 at 2:53








1




1





Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

– Sandhya Thotakura
Jan 2 at 2:53





Thanks for the answer. But I will go with W-B as I feel his answer is more robust. Thanks for teaching me the concept of zfill(int)

– Sandhya Thotakura
Jan 2 at 2:53













@SandhyaThotakura Happy coding!

– Scott Boston
Jan 2 at 2:53





@SandhyaThotakura Happy coding!

– Scott Boston
Jan 2 at 2:53











1














You have to change the datatype to string or else the initial zeroes will be trimmed.
Below is my solution. Hope it helps



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])
df = df.astype(str)
df['id'] = df['id'].apply(lambda x: x if len(x)==6 else '0'*(6-len(x))+x)

df:
id
111111
123456
012345
000234
000012
987654





share|improve this answer
























  • Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

    – Sandhya Thotakura
    Jan 2 at 2:55
















1














You have to change the datatype to string or else the initial zeroes will be trimmed.
Below is my solution. Hope it helps



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])
df = df.astype(str)
df['id'] = df['id'].apply(lambda x: x if len(x)==6 else '0'*(6-len(x))+x)

df:
id
111111
123456
012345
000234
000012
987654





share|improve this answer
























  • Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

    – Sandhya Thotakura
    Jan 2 at 2:55














1












1








1







You have to change the datatype to string or else the initial zeroes will be trimmed.
Below is my solution. Hope it helps



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])
df = df.astype(str)
df['id'] = df['id'].apply(lambda x: x if len(x)==6 else '0'*(6-len(x))+x)

df:
id
111111
123456
012345
000234
000012
987654





share|improve this answer













You have to change the datatype to string or else the initial zeroes will be trimmed.
Below is my solution. Hope it helps



df = pd.DataFrame([111111,123456,12345,234,12,987654],columns=['id'])
df = df.astype(str)
df['id'] = df['id'].apply(lambda x: x if len(x)==6 else '0'*(6-len(x))+x)

df:
id
111111
123456
012345
000234
000012
987654






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 2:40









Bhanu TezBhanu Tez

41519




41519













  • Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

    – Sandhya Thotakura
    Jan 2 at 2:55



















  • Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

    – Sandhya Thotakura
    Jan 2 at 2:55

















Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

– Sandhya Thotakura
Jan 2 at 2:55





Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.

– Sandhya Thotakura
Jan 2 at 2:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000618%2fpython-pandas-dataframe-change-content-based-on-string-length%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$