1

I have a data file in this format:

datafile

I want the columns to be grouped by month in a pivot table. When I pivot the data a column for each day is being created.

df = ex.read_excel("C:\\ExportReport.xlsx", "ExportReport")
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='Due Date', aggfunc=np.sum, fill_value=0)

Is there a way to tell pandas to group the columns by month?

3
  • any reason not to use a pivot table within Excel? Commented Apr 8, 2014 at 17:14
  • A file like this is stored on my network every week with updated data. I want to create a cron job to run that will do a bunch of things with the data and create a new report. I currently have a macro in VBA that does everything but I can't easily run this on a schedule Commented Apr 8, 2014 at 17:31
  • thanks, I go back and forth between the two, and just trying to see how others view the benefits/drawbacks Commented Apr 8, 2014 at 17:37

1 Answer 1

2

Need to have a field that calculates the month. If this is going to span multiple years, will need to combine into one field.

df['YYYY-MM'] = df['Due Date'].apply(lambda x: x.strftime("%Y-%m"))

Then try yours, but change to the monthly field...

table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='YYYY-MM', aggfunc=np.sum, fill_value=0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.