数据帧看起来像:

       OPENED
0  2004-07-28
1  2010-03-02
2  2005-10-26
3  2006-06-30
4  2012-09-21

我成功地把它们转换成了我想要的格式,但似乎效率很低。
   OPENED
0   40728
1  100302
2   51026
3   60630
4  120921

我用于日期转换的代码是:
df['OPENED'] = pd.to_datetime(df.OPENED, format='%Y-%m-%d')
df['OPENED'] = df['OPENED'].apply(lambda x: x.strftime('%y%m%d'))
df['OPENED'] = df['OPENED'].apply(lambda i: str(i))
df['OPENED'] = df['OPENED'].apply(lambda s: s.lstrip("0"))

最佳答案

您可以使用str.replace,然后按str[2:]删除前两个字符,最后按0删除前导str.lstrip

print (type(df.ix[0,'OPENED']))
<class 'str'>
print (df.OPENED.dtype)
object

print (df.OPENED.str.replace('-','').str[2:].str.lstrip('0'))
0     40728
1    100302
2     51026
3     60630
4    120921
Name: OPENED, dtype: object

如果dtype已经datetime使用strftimestr.lstrip
print (type(df.ix[0,'OPENED']))
<class 'pandas.tslib.Timestamp'>
print (df.OPENED.dtype)
datetime64[ns]

print (df.OPENED.dt.strftime('%y%m%d').str.lstrip('0'))
0     40728
1    100302
2     51026
3     60630
4    120921
Name: OPENED, dtype: object

感谢您的评论:
print (df['OPENED'].apply(lambda L: '{0}{1:%m%d}'.format(L.year % 100, L)))
0     40728
1    100302
2     51026
3     60630
4    120921
Name: OPENED, dtype: object

10-06 15:54