I have a dataframe as below, where I do a groupby on Itr
and Type
:
- How do I get the value in
Start
for the row in each group whereValues
column is max? - I only want the first max; there are multiple rows where
Values
is max (20.402). My expected answer should be 101 (because this is the time whenValues
is the max) when I do this after groupby ofItr
2 &Type
1. - I am unable to use
.loc
since I have donegroupby
. - I am unable to use
apply
function for this since it involves two columns.
Itr Type Start Values
2 1 101 20.402
2 1 102 20.402
2 1 103 20.399
2 1 104 20.399
2 1 105 20.399
2 1 106 20.383
2 1 107 20.383
2 1 108 20.383
2 1 109 20.383
2 1 110 20.383
2 1 111 20.36
2 1 112 20.36
2 1 113 20.36
2 1 114 20.36
2 1 115 20.36
2 1 116 20.36
2 1 117 20.36
2 1 118 20.36
2 1 119 20.36
2 1 120 20.36
3 1 121 20.348
3 1 122 20.348
3 1 123 20.348
3 1 124 20.348
3 1 125 20.348
3 1 126 20.34
3 1 127 20.34
3 1 128 20.34
3 1 129 20.34
3 1 130 20.34
3 1 131 20.337
3 1 132 20.337
3 1 133 20.337
3 1 134 20.337
3 1 135 20.337
3 1 136 20.342
3 2 121 20.058
3 2 122 20.058
3 2 123 20.058
3 2 124 20.058
3 2 125 20.043
3 2 126 20.043
3 2 127 20.043
3 2 128 20.043
3 2 129 20.043
3 2 130 20.035
3 2 131 20.035
3 2 132 20.035
3 2 133 20.035
3 2 134 20.035
3 2 135 20.021
As suggested I have put a simpler df & tried to make my requirement a bit more clearer.
Itr Type Time Val
2 3 101 3
2 3 102 4
2 3 103 5
2 3 104 6
2 3 105 6
2 3 106 5
2 3 107 1
1 2 101 11
1 2 102 12
1 2 103 13
1 2 104 18
1 2 105 15
1 2 106 10
4 5 101 21
4 5 102 22
4 5 103 27
4 5 104 29
4 5 105 25
4 5 106 26
I want the 'Time' and Max & Min 'Value' AFTER group by (that is within each group) in separate columns (one column for Max & one column for min)