2017-06-17 42 views
2

这里针对特定情况描述了该问题,但对于许多类似的项目来说这将是有价值的。将数字的函数应用于pandas.series的快速方法

一个pandas.series称为个月包含每个样本的月份日期的INT格式(1,2,3,4,...)。我想将它改成“01,02,03,... 12”的样式,然后再添加一年。

使用“{0:0 = 2D}” 的格式(A),该系列值可以很容易地转化:

df['date'] = np.nan 
for i in range(0,len(df),1): 
    df.date.iloc[i] = df.year.iloc[i] +"-"+'%2d'%df.month.values.iloc[i] 
### df.date is a new series contain the year-month('2017-01','2017-02') 

但循环策略是未效率,是有没有简单的方法来实现相同的目标?

回答

3

您可以转换个月str类型,然后使用str.zfill

month = pd.Series([1,2,12]) 

month.astype(str).str.zfill(2) 

#0 01 
#1 02 
#2 12 
#dtype: object 

要使用一年串连它:

df.year.astype(str) + '-' + df.month.astype(str).str.zfill(2) 
4

您可以使用apply

month.apply("{0:0=2d}".format) 

TIMIN克

  • Psidom的方法

%timeit month.astype(STR).str.zfill(2)

10循环,最好的3:每次循环39.1毫秒

  • 此方法:

%timeit month.apply( “{0:0 = 2D}” 的格式。)

100个循环,最好的3:每次循环7.93毫秒

df = pd.DataFrame({'month':pd.np.random.randint(1,12,10000),'year':pd.np.random.choice([i for i in range(2004,2017)],10000)}) 

df.year.astype(str) + '-' + df.month.apply("{0:0=2d}".format) 

输出:

0  2014-10 
1  2012-04 
2  2015-03 
3  2014-05 
4  2007-03 
5  2008-04 
2

您可以在具有相应命名列的数据框上使用pd.to_datetime来创建一系列日期时间对象。

考虑数据框df

df = pd.DataFrame(dict(year=[2011, 2012], month=[3, 4])) 
df 

    month year 
0  3 2011 
1  4 2012 

所有我们缺少的是day列。如果再加上它,我们可以把它传递给pd.to_datetime

pd.to_datetime(df.assign(day=1)) 

0 2011-03-01 
1 2012-04-01 
dtype: datetime64[ns] 

嗯,这很方便。怎么办?

pd.to_datetime(df.assign(day=1)).apply('{:%Y-%m}'.format) 

0 2011-03 
1 2012-04 
dtype: object 

或者

pd.to_datetime(df.assign(day=1)).dt.strftime('%Y-%m') 

0 2011-03 
1 2012-04 
dtype: object 

创建一个新的列

df.assign(year_month=pd.to_datetime(df.assign(day=1)).dt.strftime('%Y-%m')) 

    month year year_month 
0  3 2011 2011-03 
1  4 2012 2012-04 

但是,我们可以刚才做

df.assign(year_month=df.apply(lambda x: '{year}-{month:02d}'.format(**x), 1)) 

    month year year_month 
0  3 2011 2011-03 
1  4 2012 2012-04 
+1

并在效率方面;字符串方法可能会在构建阶段胜过这一点,但随着日期的推移,事后您可能会更快更容易。 – ayhan