getting another column is one column of data has maximum value in python? - python-3.x

I have a data frame
date object price
190403 a 1
190405 a 23
190506 b -4
190507 d 56
I want to get a date of a column having a maximum Price i.e 190507
expected output
190507

For scalar output, always one max date value use Series.idxmax with convert date to index by DataFrame.set_index:
df.set_index('date')['price'].idxmax()
If want all max values in Series use boolean indexing and compare all values by max, DataFrame.loc is fir also filtering date column:
df.loc[df['price'].eq(df['price'].max()), 'date']

You can subset the df so that you only have the row where the price column is at its maximum, and then choose the date column:
df[df.price==df.price.max()].date

Related

calculate average of values in column based on target values

I have this sample of a data frame. I would like to find an average value for each assignment type value according to the target value. For example, for rows with pass result, I want to calculate their average C and T. There are about 5 Cs, and 3 Ts based on time. id_assigment differentiate between them you can see, but I want to find the average for each C and T for each class value. For example, average C with id 45 for Pass rows, for Fail rows,etc. I wonder how to calculate the averages?
Try pd.pivot_table
pd.pivot_table(df, columns=['id_assignemnt', 'assignemnt_type', 'result'], values='score')

Power Query m Language Convert List to Single String Value with Separator

{"Top 3 Max Sales Per Day", each Text.Combine(List.Transform(List.MaxN([Item Quantity Final], 3), each Number.ToText(_),",")), type number}
The Output Column is Still providing Error in each cell and I can't get the desired result to have the top 3 max values in one cell

Spotfire over function - previous date with multiple categories

I'd like to calculate a value difference between two rows that is classified by Type and ID and is sorted by a date column. See example data table. Note that column "Calculated" shows the current results, while column "Expected" shows the results I would like to achieve.
ID Type Date ValueA ValueB ValueC Calculated Expected
1 A 8/15/2017 38.11
1 A 8/15/2017 78.10 39.99
1 A 8/22/2017 110.24 32.14
1 B 8/22/2017 20
1 B 9/16/2017 10
1 A 9/16/2017 101.13 -9.11
2 C 8/17/2012 90
2 A 8/18/2012 863.25
2 B 8/18/2012 15
2 A 8/19/2012 952.35 89.1 89.1
2 B 8/19/2012 20
I tried the following custom expression, but it seems to calculate a difference value only for cases where there is a consecutive date for a given ID.
Case
when [Type]="A" then [Value] - Max([Value]) over (Intersect([ID],Previous([Date])))
else NULL
The expression is an attempt to filter to Type "A" for the OVER statement, such that the previous date is the previous date only for values categorized as Type "A". However, it seems to consider the previous date as NULL if is not a consecutive date (i.e. prior day). See "Calculated" in table above for results from this expression.
I also tried to add Type to the Intersect statement, e.g. Intersect([ID],[Type], Previous([Date]), but I get a similar result.
does
[value] - max[value] over (intersect([Type],[ID],previous([Date])))
give you the calculated column you want
Edit:
I was able to almost match your expected column using this formula (previousperiod instead of just previous), but the first two rows are the same date so it is not an exact match.
formula

Return value in cell based on other cells values combination in excel

I have a table in Excel that has the following columns:
My table
dm: Can be 0 or 1
gdr: Can be 0 or 1
smk: Can be 0 or 1
agemin: min age number
agemax: max age number
sbpmin: min sbp number
sbpmax: max sbp number
chlmin: min chl number
chlmax: max chl number
The table is big with all possible combinations.
What i need is a way to find the value in result based on the input of:
dm, gd, smk, age, sbp and chl. As i mention the first 3 can be a 0 or a 1 but the other 3 is a number that must be contain in the range given by the columns min and max.
Has anyone have a clue on how can i solve this?
Thanks,
Using the provided table and assuming the parameters for a lookup are in column M (as shown in the below picture), then the formula in cell M9 and copied right to get the result is:
=IFERROR(INDEX($J$2:$J$4,MATCH(1,INDEX((M2=$A$2:$A$4)*(M3=$B$2:$B$4)*(M4=$C$2:$C$4)*(M5>=$D$2:$D$4)*(M5<=$E$2:$E$4)*(M6>=$F$2:$F$4)*(M6<=$G$2:$G$4)*(M7>=$H$2:$H$4)*(M7<=$I$2:$I$4),),0)),"No matches found")

Excel Formula - get the date of a maximal value within a list

I have a rather big (long) table and need to do something quite simple but I'm currently with a sort of blackout...
Imagine that you have:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
... ... ... ... ... ... ????
I want to deploy in ???? a formula that will result in the date at which the maximal value (between Value 1...3) was registered. For instance:
Date 1 Value 1 Date 2 Value 2 Date 3 Value 3 Date of MAX
20160501 10 20160722 47 20161002 9 20160722
meaning, the Date of MAX is of Date 2 because that was the date at which the MAX was measured.
Thanks in advance.
You could do a more general solution using offset:-
=MAX(N(OFFSET(D2,0,COLUMN(A:D)*3)))
to find the largest value - put this in (say) R2.
Then find a match for it and pick out the corresponding date:-
=OFFSET(C2,0,MATCH(R2,N(OFFSET(D2,0,COLUMN(A:D)*3)),0)*3)
assuming the dates and values are every third column.
These are array formulae and must be entered with CtrlShiftEnter
If the last value really was in P21 you would have to give a row offset as well as a column offset.
OK, I found a dirty but simple solution (don't know why I didn't think of it at first):
=IF(G2>=MAX(G2,J2,M2,P21),F2,IF(J2>=MAX(G2,J2,M2,P21),I2,IF(M2>=MAX(G2,J2,M2,P21),L2,O2)))
where the pairs (4 of them) are at FG, IJ, LM, OP. Not elegant but does the job.

Resources