根据字符串长度追加
Append based on length of string
我有一个像这样的 df:
Year Month Day
1984 1 1
1985 12 22
我想让 Month
和 Day
无论如何都有两位数。所以我想要的数据框是这样的:
Year Month Day
1984 01 01
1985 12 22
我一直在玩这个:
for i in df.Month:
i=str(i)
if len(i) < 2:
i='0' + i
print i
但我不确定如何将新值实际重新插入到数据框中,而且我很确定首先有更好的方法来做到这一点
你应该让那些 DataFrame
保持原样,只有当你需要生成报告时你才应该担心这个演示问题。
然后就变成了一般的字符串格式问题(see also format()
)。下面显示的是 (1) 转换为字符串,(2) 用前导空格填充到长度两个,(3) 用前导零填充到长度两个:
>>> ['{}'.format(x) for x in range(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> ['{:2}'.format(x) for x in range(10)]
[' 0', ' 1', ' 2', ' 3', ' 4', ' 5', ' 6', ' 7', ' 8', ' 9']
>>> ['{:02}'.format(x) for x in range(10)]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
我认为使用 to_datetime
创建日期列以使用 numpy 日期时间数据类型可能是个好主意。它实际上看起来会给你接近你想要的格式,但是你也可以从这里使用你想要的任何格式格式化你的日期 dt.strftime
:
df['Date'] = pd.to_datetime(df.Year.astype(str) + ' '
+ df.Month.astype(str) + ' '
+ df.Day.astype(str))
df['Date']
0 1984-01-01
1 1985-12-22
Name: Date, dtype: datetime64[ns]
df.Date.dt.strftime("%Y %m %d")
0 1984 01 01
1 1985 12 22
请试试这个选项。如果您希望月份和日期各有 2 位数字。
for i in df.Month:
i=str(i)
print('%02d'%(i,)) #this is for python 3.4.4 in previous version this may be print "%02d" % (i,)
可以使用astype
for converting to string
and zfill
来填充0
:
#df['Year'] = df['Year'].astype(str) #if column Year has to be string
df['Month'] = df['Month'].astype(str).str.zfill(2)
df['Day'] = df['Day'].astype(str).str.zfill(2)
print df
Year Month Day
0 1984 01 01
1 1985 12 22
如果所有列的 type
必须转换为 string
:
df = df.astype(str)
df['Month'] = df['Month'].str.zfill(2)
df['Day'] = df['Day'].str.zfill(2)
print df
时间:
In [225]: %timeit df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
1 loops, best of 3: 500 ms per loop
In [226]: %timeit a(df)
100 loops, best of 3: 10.8 ms per loop
代码:
df1 = df.copy()
def a(df):
df = df.astype(str);
df['Month'] = df['Month'].str.zfill(2);
df['Day'] = df['Day'].str.zfill(2);
return df
print df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
print a(df)
我有一个像这样的 df:
Year Month Day
1984 1 1
1985 12 22
我想让 Month
和 Day
无论如何都有两位数。所以我想要的数据框是这样的:
Year Month Day
1984 01 01
1985 12 22
我一直在玩这个:
for i in df.Month:
i=str(i)
if len(i) < 2:
i='0' + i
print i
但我不确定如何将新值实际重新插入到数据框中,而且我很确定首先有更好的方法来做到这一点
你应该让那些 DataFrame
保持原样,只有当你需要生成报告时你才应该担心这个演示问题。
然后就变成了一般的字符串格式问题(see also format()
)。下面显示的是 (1) 转换为字符串,(2) 用前导空格填充到长度两个,(3) 用前导零填充到长度两个:
>>> ['{}'.format(x) for x in range(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> ['{:2}'.format(x) for x in range(10)]
[' 0', ' 1', ' 2', ' 3', ' 4', ' 5', ' 6', ' 7', ' 8', ' 9']
>>> ['{:02}'.format(x) for x in range(10)]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
我认为使用 to_datetime
创建日期列以使用 numpy 日期时间数据类型可能是个好主意。它实际上看起来会给你接近你想要的格式,但是你也可以从这里使用你想要的任何格式格式化你的日期 dt.strftime
:
df['Date'] = pd.to_datetime(df.Year.astype(str) + ' '
+ df.Month.astype(str) + ' '
+ df.Day.astype(str))
df['Date']
0 1984-01-01
1 1985-12-22
Name: Date, dtype: datetime64[ns]
df.Date.dt.strftime("%Y %m %d")
0 1984 01 01
1 1985 12 22
请试试这个选项。如果您希望月份和日期各有 2 位数字。
for i in df.Month:
i=str(i)
print('%02d'%(i,)) #this is for python 3.4.4 in previous version this may be print "%02d" % (i,)
可以使用astype
for converting to string
and zfill
来填充0
:
#df['Year'] = df['Year'].astype(str) #if column Year has to be string
df['Month'] = df['Month'].astype(str).str.zfill(2)
df['Day'] = df['Day'].astype(str).str.zfill(2)
print df
Year Month Day
0 1984 01 01
1 1985 12 22
如果所有列的 type
必须转换为 string
:
df = df.astype(str)
df['Month'] = df['Month'].str.zfill(2)
df['Day'] = df['Day'].str.zfill(2)
print df
时间:
In [225]: %timeit df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
1 loops, best of 3: 500 ms per loop
In [226]: %timeit a(df)
100 loops, best of 3: 10.8 ms per loop
代码:
df1 = df.copy()
def a(df):
df = df.astype(str);
df['Month'] = df['Month'].str.zfill(2);
df['Day'] = df['Day'].str.zfill(2);
return df
print df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
print a(df)