我希望使用 python pandas 将 df 中的日期更改为新列中的会计月份。
这是一个例子
17/01/2016 201601
18/01/2016 201602
你能帮我获取所需的Python代码吗?
最佳答案
你可以这样做:
In [29]: df['fiscal_month'] = np.where(df.Date.dt.day < 18, df.Date, df.Date + pd.DateOffset(months=1))
In [30]: df
Out[30]:
Date new fiscal_month
0 2016-01-17 2016-01-17 2016-01-17
1 2016-01-18 2016-02-01 2016-02-18
In [31]: df.fiscal_month = df.fiscal_month.dt.strftime('%Y%m')
In [32]: df
Out[32]:
Date new fiscal_month
0 2016-01-17 2016-01-17 201601
1 2016-01-18 2016-02-01 201602
关于python - 将日期更改为财政月份 pandas python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046935/