问题
I have a dataframe that I have grouped by multiple columns. Within each group, I would like to then generate a value that finds the last entity of each of those groups and divide by the first entity. I would also like to show the number of entities and the last entity value in the output.
See below for an example data and the desired output. I know how to show the count of the group, shown below in the code.
df_group=df.groupby(['ID','Item','End_Date','Type'])
df_output=df_group.size().reset_index(name='Group Count')
Below, I am grouping by the below:
So the first row in the example output dataframe that I am seeking has the Final Value of 2 (the most recent value for the group), and a percent change of the last value of 2 divided by the first value of 3. Two more examples are shown as well.
Please let me know if you have any tips on how to go about this application to a groupby object. Thank you very much for your help.
回答1:
Just do assign with groupby tail and head
df_group=df.groupby(['ID','Item','End_Date','Type'])
df_output=df_group.size().reset_index(name='Group Count')
df_output['PCTCHange']=((df_group.value.tail(1)/df_group.value.head(1))-1).values
df_output['FinalValue']=df_group.value.tail(1).values
来源:https://stackoverflow.com/questions/53164899/pandas-groupby-divide-last-in-group-by-first-in-group