如何在 Pandas 中为字符串添加前导零格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33651668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add leading zero formatting to string in Pandas?
提问by Student
Objective:To format ['Birth Month']
with leading zeros
目标:['Birth Month']
用前导零格式化
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:
这产生了一个值列表,['Birth Month']
其中不是我需要的:
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']
:
相反,我正在寻找如下所示的值和格式['Birth Month']
:
A B Birth Year Birth Month
0 1 4 1912 04
1 2 5 1989 12
2 3 6 1921 09
回答by EdChum
Cast the dtype of the series to str
using astype
and use vectorised str.zfill
to pad with 0
:
将系列的 dtype 投射到str
usingastype
并使用矢量化str.zfill
来填充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:
您所做的只是分配一个标量值(这就是为什么每一行都相同的原因)并将元素转换为列表的 str :
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]'