简体   繁体   中英

How to find maximum value in Pandas data frame and assign a new Value to it?

This is my pandas data frame pandas data frame

ID          Position   Time(in Hours) Date
01          18          2             01/01/2016
01          21          4             01/10/2016
01          19          2             01/10/2016
05          19          5             01/10/2016
05          21          1             01/10/2016
05          19          8             01/10/2016
02          19          18            02/10/2016
02          35          11            02/10/2016

I need to assign '1' for the maximum Time for each Id and Date else assign '0'. My code is

def find_max(db7):
max_row = db7['Time'].max()
labels = np.where((db7['Time_in_Second'] == max_row),'1','0')
return max_row

db7['Max'] = db7['Time'].map(find_max)

But I'm getting below error. How do I do this please?

TypeError: 'float' object is not subscriptable

My Expected out put should be:

ID          Position   Time(in Hours) Date        Max
01          18          2             01/01/2016  0
01          21          4             01/10/2016  1
01          19          2             01/10/2016  0
05          19          5             01/10/2016  0
05          21          1             01/10/2016  0
05          19          8             01/10/2016  1
02          19          18            02/10/2016  1
02          35          11            02/10/2016  0

Use groupby with transform max and numpy.where for assign new values:

max1 = db7.groupby(['ID','Date'])['Time(in Hours)'].transform('max')
db7['Max'] = np.where(db7['Time(in Hours)'].eq(max1), '1', '0')
print (db7)
   ID  Position  Time(in Hours)        Date Max
0   1        18               2  01/01/2016   1
1   1        21               4  01/10/2016   1
2   1        19               2  01/10/2016   0
3   5        19               5  01/10/2016   0
4   5        21               1  01/10/2016   0
5   5        19               8  01/10/2016   1
6   2        19              18  02/10/2016   1
7   2        35              11  02/10/2016   0

Or convert True s and False s to '1' and '0' by double astype :

max1 = db7.groupby(['ID','Date'])['Time(in Hours)'].transform('max')
db7['Max'] = db7['Time(in Hours)'].eq(max1).astype(int).astype(str)
print (db7)
   ID  Position  Time(in Hours)        Date  Max
0   1        18               2  01/01/2016    1
1   1        21               4  01/10/2016    1
2   1        19               2  01/10/2016    0
3   5        19               5  01/10/2016    0
4   5        21               1  01/10/2016    0
5   5        19               8  01/10/2016    1
6   2        19              18  02/10/2016    1
7   2        35              11  02/10/2016    0

Detail:

print (max1)
0     2
1     4
2     4
3     8
4     8
5     8
6    18
7    18
Name: Time(in Hours), dtype: int64

#eq is same as ==
print (db7['Time(in Hours)'].eq(max1))
0     True
1     True
2    False
3    False
4    False
5     True
6     True
7    False
Name: Time(in Hours), dtype: bool

EDIT:

If need group by only column ID :

max1 = db7.groupby('ID')['Time(in Hours)'].transform('max')
db7['Max'] = np.where(db7['Time(in Hours)'].eq(max1), '1', '0')
print (db7)
   ID  Position  Time(in Hours)        Date Max
0   1        18               2  01/01/2016   0
1   1        21               4  01/10/2016   1
2   1        19               2  01/10/2016   0
3   5        19               5  01/10/2016   0
4   5        21               1  01/10/2016   0
5   5        19               8  01/10/2016   1
6   2        19              18  02/10/2016   1
7   2        35              11  02/10/2016   0

print (max1)
0     4
1     4
2     4
3     8
4     8
5     8
6    18
7    18
Name: Time(in Hours), dtype: int64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM