熊猫-根据日期时间列的值创建一个新的字符串列

时间:2020-04-01 06:21:50

标签: python pandas

以下代码创建一个新的列季节,该列根据日期列的值分配一个“季节”。

df2.season = None
df2.loc[(df2.date=='2010-02-22 00:00:00'), 'season'] = '2009/2010'
df2.loc[(df2.date=='2011-02-22 00:00:00'), 'season'] = '2010/2011'
df2.loc[(df2.date=='2014-09-19 00:00:00'), 'season'] = '2014/2015'
df2.loc[(df2.date=='2012-02-22 00:00:00'), 'season'] = '2011/2012'
df2.loc[(df2.date=='2013-09-20 00:00:00'), 'season'] = '2013/2014'
df2.loc[(df2.date=='2015-09-10 00:00:00'), 'season'] = '2015/2016'
df2.head()

有没有一种方法可以用循环更少的代码行来做到这一点?

我尝试过pd.cut(),但是由于date列中的datetime值而引发了错误。

我想象有一种压缩日期和季节值,然后使用循环的方法,但是我不知道该怎么做。谢谢!

1 个答案:

答案 0 :(得分:0)

如果逻辑上为六月最后一天之前和之后的日期时间,则使用:

df["date"] = pd.to_datetime(df["date"])

y = df["date"].dt.year
y1 = y.astype(str)
y2 = (y - 1).astype(str)
y3 = (y + 1).astype(str)

df["season"] = np.where(df["date"].dt.month > 6, y1 + '/' + y3, y2 + '/' + y3)
print (df)
        date     season
0 2010-02-22  2009/2011
1 2011-02-22  2010/2012
2 2014-09-19  2014/2015
3 2012-02-22  2011/2013
4 2013-09-20  2013/2014
5 2015-09-10  2015/2016