Given a dataframe containing columns related to several months in a year, I need to perform a series of identical operations on per-month slices of this dataframe and output dataframes for each month period containing the original values along with columns containing the values returned from the operations performed on the slice in question.
Question is how to set up these slices (after which the operations can be performed on the dataframes) without having to define each dataframe related to a particular month (the solution needs to be flexible to accommodate for changing periods).
Input:
df_a_number
Index 21-Nov 21-Dec 22-Jan 22-Feb
John 2 3 1 5
Anna 1 4 3 8
df_b_letter
Index 21-Nov 21-Dec 22-Jan 22-Feb
John a f j p
Anna b b w g
Desired output:
df_dec
Index 21-Nov 21-Dec 21-Dec-Diff 21-Dec-Letter
John 2 3 1 f
Anna 1 4 3 b
df_jan
Index 21-Dec 21-Jan 22-Jan-Diff 22-Jan-Letter
John 3 1 -2 j
Anna 4 3 -1 w
And so forth for Feb...
In addition to performing the calculation in the -Diff
column and the merge in the -Letter
column, the df needs to be filtered also before outputting to file.
Any suggestions or ideas would be much appreciated.