Python pandas dataframe change content based on string length
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
add a comment |
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
Perhaps take a look at stackoverflow.com/questions/134934/…
– sfjac
Jan 2 at 2:41
add a comment |
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
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
python pandas dataframe
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.
– Sandhya Thotakura
Jan 2 at 2:55
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.
– Sandhya Thotakura
Jan 2 at 2:55
add a comment |
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
Thanks Bhanu for your answer. You are quick one to answer my question. so 1 upvote.
– Sandhya Thotakura
Jan 2 at 2:55
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Perhaps take a look at stackoverflow.com/questions/134934/…
– sfjac
Jan 2 at 2:41