简体   繁体   English

如何使用 Pandas dataframe 从 4 月开始计算财政年度的年初至今(YTD)?

[英]How to calculate year to date (YTD) of a financial year starting from month April using Pandas dataframe?

I want to calculate YTD of a financial year using Pandas dataframe.我想使用 Pandas dataframe 计算一个财政年度的 YTD。 I used below code to find it.But I got YTD from month January.我使用下面的代码来找到它。但是我从一月份开始得到 YTD。

report_table['ytd_sales'] = report_table.groupby(['year','month', 'planttype', 'market', 'product'])['sales'].cumsum()

Can anyone help me to calculate YTD fom month April to March(financial year).谁能帮我计算从四月到三月(财政年度)的年初至今。

Create column of financial_year by numpy.where :通过numpy.where创建financial_year年度列:

report_table['financial_year'] = np.where(report_table['month'] > 3,  
                                          report_table['year']+1, 
                                          report_table['year'])

Or crete datetime by both columns month and year and then convert to financial_year :monthyear列创建日期时间,然后转换为financial_year

report_table['financial_year']=(pd.to_datetime(report_table[['month','year']].assign(day=1))
                                  .dt.to_period('Q-MAR')
                                  .dt.qyear)

report_table['ytd_sales'] = report_table.groupby(['financial_year', 'planttype', 'market', 'product'])['sales'].cumsum()

USE -利用 -

report_table['Fiscal Year'] = report_table['Date'].dt.to_period('Q-MAR').dt.qyear
report_table['ytd_sales'] = report_table.groupby(['Fiscal Year',month, 'planttype', 'market', 'product'])['sales'].cumsum()

Example-例子-

import pandas as pd
dates = {'Date': ['1/1/2021', '1/31/2021', '2/1/2021', '2/28/2021', '3/1/2021', '3/31/2021', '4/1/2021', '4/30/2021', '5/1/2021', '5/31/2021', '6/1/2021']}
df = pd.DataFrame(dates)
df['Date'] = pd.to_datetime(df['Date'])
print(df)
df['Fiscal Year'] = df['Date'].dt.to_period('Q-MAR').dt.qyear

Ref link- https://datagy.io/pandas-fiscal-year/参考链接- https://datagy.io/pandas-fiscal-year/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Pandas 数据框计算月初至今 (MTD) 和 YTD? - How to calculate Month to Date (MTD) and YTD using Pandas dataframe? 从 Pandas 中的年初至今 (YTD) 数字得出月初至今 (MTD) - Derive Month to Date (MTD) from Year to Date (YTD) numbers in Pandas 如何用日期(年/月)绘制pandas DataFrame? - How to plot pandas DataFrame with date (Year/Month)? 如何从熊猫数据框中提取日期/年/月? - How do I extract the date/year/month from pandas dataframe? 从熊猫数据框中的“年-月-日”格式中删除年份 - Drop the year from "Year-month-date" format in a pandas dataframe 如何从开始日期算起一年找到Pandas DataFrame的平均值? - How to find the mean of a Pandas DataFrame exactly a year from the starting date? Python-Pandas-Datetime-如何将财政年度和财政月份转换为日历日期 - Python-Pandas-Datetime- How to convert Financial Year and Financial Month to Calendar date 如何在熊猫数据框中将日期类型从“year_week”更改为“day_month_year”格式? - How to change date type from 'year_week' to 'day_month_year' format in pandas dataframe? 修改熊猫数据框以列出年份的月份和日期 - Modify Pandas dataframe to list year month and date 如何使用日期、月份和年份填充数据框并操作该数据框? - How to populate a dataframe using Date, Month and Year and manipulate that dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM