Home > Enterprise >  Printing the whole row of my data from a max value in a column
Printing the whole row of my data from a max value in a column

Time:04-11

I am trying to select the highest value from this data but i also need the month it comes from too, here printing the whole row. Currently i'm using df.max() which just pulls the highest value. Does anyone know how to do this in pandas.

#current code
accidents["month"] = accidents.Date.apply(lambda s: int(s.split("/")[1]))
temp = accidents.groupby('month').size().rename('Accidents')

#selecting the highest value from the dataframe
temp.max()

answer given = 10937
answer i need should look like this (month and no of accidents): 11    10937

temp dataframe;

month
1      9371
2      8838
3      9427
4      8899
5      9758
6      9942
7     10325
8      9534
9     10222
10    10311
11    10937
12     9972
Name: Accidents, dtype: int64

would also be good to rename the accidents column to accidents is anyone can help too. Thanks

CodePudding user response:

If the value is unique (in your case it is) you can simply get a subset of the dataframe.

temp[temp.iloc[:,1]==temp.iloc[:,1].max()]

So what the code is doing is looking at the integer position (rows then columns) and matching it with your condition, which is the max temp.

  • Related