in Education by
There are different types of group level available in the actual data set, We would be required different types of group-wise report. (Note: group wise report is not simple hence please read below example carefully) Requirement Example when we need to report 4th level group report then need to sum data field till the last value before 4th level. The report number might any number given in the "Group" column. (Kindly refer Expected output for more clarity) For Rest of the filed, treatment as below: purchase_date, Info1 & Info2: Pickup date available at the group level data : Sum Actual Database: Group purchase_date Info1 Info2 data 2 03/23/2019 A B 57 3 03/24/2019 A B 48 4 03/25/2019 A B 40 1 03/26/2019 Q B 60 2 03/27/2019 Q B 42 3 03/28/2019 Q B 33 4 03/29/2019 Q B 36 1 03/30/2019 R B 54 2 03/31/2019 R B 57 3 04/01/2019 R B 53 4 04/02/2019 R B 56 1 04/03/2019 A B 48 2 04/04/2019 A B 40 3 04/05/2019 A B 45 4 04/06/2019 A B 60 1 04/07/2019 A B 38 2 04/08/2019 A B 58 Expected output after grouping data as per the above requirement: 4th group level report: Group purchase_date Info1 Info2 data 4 03/25/2019 A B 145 4 03/29/2019 Q B 171 4 04/02/2019 R B 220 4 04/06/2019 A B 193 4 04/08/2019 A B 96 2nd group level Report Group purchase_date Info1 Info2 data 2 03/23/2019 A B 57 2 03/27/2019 Q B 190 2 03/31/2019 R B 180 2 04/04/2019 A B 197 2 04/08/2019 A B 201 I have tried it with group by function but I am not able to Kick-off it. Can anyone help me to get the desired output? Thanks in advance. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
With the following function f(z, l) with parameter l being the required level and z being your DataFrame: def f(z, l): x = (z['Group'] == l) | (z.index == z.index[-1]) cs = z['data'].cumsum() dx = cs - cs.where(x, np.nan).ffill().shift(1).fillna(0) return z[x].drop('data', 1).assign(Group=l).join(dx) print(f(df, 2)) print(f(df, 4)) Output: Group purchase_date Info1 Info2 data 0 2 03/23/2019 A B 57.0 4 2 03/27/2019 Q B 190.0 8 2 03/31/2019 R B 180.0 12 2 04/04/2019 A B 197.0 16 2 04/08/2019 A B 201.0 Group purchase_date Info1 Info2 data 2 4 03/25/2019 A B 145.0 6 4 03/29/2019 Q B 171.0 10 4 04/02/2019 R B 220.0 14 4 04/06/2019 A B 193.0 16 4 04/08/2019 A B 96.0 It basically calculates cumulative sums with cumsum resetting them every time we hit a record with the given level or the last record in the DataFrame. Update: to make the last row in purchase_date always equal to previous row + 4 days: def f(z, l): x = (z['Group'] == l) | (z.index == z.index[-1]) cs = z['data'].cumsum() dx = cs - cs.where(x, np.nan).ffill().shift(1).fillna(0) zz = z[x].drop('data', 1).assign(Group=l).join(dx) zz['purchase_date'] = pd.to_datetime(zz['purchase_date']) zz.at[zz.index[-1], 'purchase_date'] = zz.at[zz.index[-2], 'purchase_date'] + pd.Timedelta('4d') return zz

Related questions

0 votes
0 votes
    router examine _____ of given data package and forward table to device best way to transfer data packets Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    Is HTML5 backward compatible with old browsers? A - true B - false...
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    Grouping of data can be done by ________ element of lens. A. Measures B. Filters C. Dimensions...
asked Nov 1, 2022 in Education by JackTerrance
0 votes
    In sklearn, GridSearchCV can take a pipeline as a parameter to find the best estimator through cross ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    The problem is that I cannot make a script that makes the enemy rotate so that "up" is pointing against the ... something simple... Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to get the number of rows of dataframe df with Pandas, and here is my code. Method 1: total_rows ... What am I doing wrong? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    How to replace the values of NA with zeros in some column and data frame I have? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    what is the difference between sum and cumulative sum? how do you perform the two on dataframe? Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    How to replace the values of NA with zeros in some column and data frame I have? Select the correct ... Questions, Core Hadoop MCQ,core Hadoop interview questions for experienced...
asked Oct 30, 2021 in Education by JackTerrance
0 votes
    What is the difference between back-propagation and feed-forward neural networks? By googling and reading, I found ... feed-forward? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I'm making a basic feedforward neural network to solve the XOR gate problem. Standard settings: input layer + hidden layer + ... outputData_samples : Array = Array() for i in 0.....
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    What is the purpose of the method forward()? (a) Loads any random URL in the history list (b) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...