0

I'm trying to find in which row does the maximum value is in? I have a dataframe named recent_grads. Within it there is a column named share_women which has been set equal to sw_col. I also have the maximum value of that column and set equal to a variable called max_sw.

Tried the following code:

print(recent_grads['sharewomen'] == max_sw)

and I get the following result:

0      False
1      False
2      False
3      False
4      False
5      False
6      False
7      False
8      False
9      False
10     False
11     False
12     False
13     False
14     False
15     False
16     False
17     False
18     False
19     False
20     False
21     False
22     False
23     False
24     False
25     False
26     False
27     False
28     False
29     False
       ...  
143    False
144    False
145    False
146    False
147    False
148    False
149    False
150    False
151    False
152    False
153    False
154    False
155    False
156    False
157    False
158    False
159    False
160    False
161    False
162     True
163    False
164    False
165    False
166    False
167    False
168    False
169    False
170    False
171    False
172    False
Name: sharewomen, Length: 173, dtype: bool

this the full view of sw_col:

0      0.120564
1      0.101852
2      0.153037
3      0.107313
4      0.341631
5      0.144967
6      0.535714
7      0.441356
8      0.139793
9      0.437847
10     0.199413
11     0.196450
12     0.119559
13     0.310820
14     0.183985
15     0.320784
16     0.343473
17     0.252960
18     0.350442
19     0.236063
20     0.578766
21     0.222695
22     0.325092
23     0.292607
24     0.278790
25     0.227118
26     0.342229
27     0.322222
28     0.189970
29     0.251389
         ...   
143    0.606889
144    0.423209
145    0.779933
146    0.444582
147    0.506721
148    0.845934
149    0.667034
150    0.752144
151    0.810704
152    0.910933
153    0.697384
154    0.798920
155    0.905590
156    0.904075
157    0.745662
158    0.728495
159    0.584776
160    0.383719
161    0.719974
162    0.968954
163    0.707136
164    0.967998
165    0.690111
166    0.629505
167    0.666119
168    0.637293
169    0.817099
170    0.799859
171    0.798746
172    0.877960
Name: sharewomen, Length: 173, dtype: float64

any idea what to do? if so please try to do it that involve these three guys: recent_grads, max_sw, and sw_col.

2 Answers2

1

I believe you're looking for idfmax:

df = pd.DataFrame([1, 4, 3, 2, 5, 3])
print(df) 

   0
0  1
1  4
2  3
3  2
4  5
5  3


df.idxmax()
==> 
0    4
dtype: int64

Alternatively, if for some reason you're not allowed to use idxmax, you can do the following (note that the name of the relevant column is '0'):

df[df[0] == df[0].max()].index.values[0]

The output is 4.

Roy2012
  • 11,755
  • 2
  • 22
  • 35
0

Use idxmax:

recent_grads.iloc[recent_grads["share_women"].idxmax()]
Ozzy08
  • 155
  • 1
  • 9