6

Objective: To format ['Birth Month'] with leading zeros

Currently, I have this code:

import pandas as pd
import numpy as np

df1=pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
df1['Birth Year']= np.random.randint(1905,1995, len(df1))
df1['Birth Month']= str(np.random.randint(1,12, len(df1))).zfill(2)
df1

Which produces a list of values in ['Birth Month'] which is not what I need:

    A   B   Birth Year  Birth Month
0   1   4   1912        [4 5 9]
1   2   5   1989        [4 5 9]
2   3   6   1921        [4 5 9]

Instead, I am looking for values and formatting like the following in ['Birth Month']:

    A   B   Birth Year  Birth Month
0   1   4   1912        04
1   2   5   1989        12
2   3   6   1921        09

1 Answer 1

10

Cast the dtype of the series to str using astype and use vectorised str.zfill to pad with 0:

In [212]:
df1=pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
df1['Birth Year']= np.random.randint(1905,1995, len(df1))
df1['Birth Month']= pd.Series(np.random.randint(1,12, len(df1))).astype(str).str.zfill(2)
df1

Out[212]:
   A  B  Birth Year Birth Month
0  1  4        1940          09
1  2  5        1945          04
2  3  6        1962          03

All you did was assign a scalar value (which is why every row is the same) and convert the element to a str of a list:

In [217]:
df1['Birth Month'].iloc[0]

Out[217]:
'[3 6 9]'

You can see the result of the assignment here broken down:

In [213]:
(np.random.randint(1,12, len(df1)))

Out[213]:
array([5, 7, 4])

In [214]:
str(np.random.randint(1,12, len(df1))).zfill(2)

Out[214]:
'[2 9 5]'
Sign up to request clarification or add additional context in comments.

3 Comments

EdChum - using your code I ran into an AttributeError: 'StringMethods' object has no attribute 'zfill'. I am using Python 2.7.10.
What version of pandas are you using?
EdChum - I updated pandas to the current version and your solution worked perfectly. Thank you.

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.