I have a DataFrame
named au
from which I want to drop the rows where date
aligns with observations where bal==bal.max()
. For example, if bal==bal.max()
is associated with 2009-08-01
, then I want to drop all other observations for which date=='2009-08-01'
. Below is what I've tried but both attempts result in ValueError: Series lengths must match to compare
au = au[au.date != au.date[au.bal==au.bal.max()]]
au = au[au.date != au.date[au.bal==au.bal.max()].values]
Using idxmax
and @jezrael's setup
setup
au = pd.DataFrame({'bal':[1,2,3,4],
'date':['2009-08-01','2009-08-01','2009-08-02', '2009-08-02'],
'C':[7,8,9,1]})
solution
dmax = au.date.loc[au.bal.idxmax()]
au[au.date != dmax]
C bal date
0 7 1 2009-08-01
1 8 2 2009-08-01
I think you need get scalar from one item Series
by item
or values
with selected firts value by [0]:
au = pd.DataFrame({'bal':[1,2,3,4],
'date':['2009-08-01','2009-08-01','2009-08-02', '2009-08-02'],
'C':[7,8,9,1]})
print (au)
C bal date
0 7 1 2009-08-01
1 8 2 2009-08-01
2 9 3 2009-08-02
3 1 4 2009-08-02
print (au[au.date != au.loc[au.bal==au.bal.max(), 'date'].item()])
C bal date
0 7 1 2009-08-01
1 8 2 2009-08-01
Solution with idxmax
- create Series
with date
first:
print (au.set_index('date').bal.idxmax())
2009-08-02
au = au[au.date != au.set_index('date').bal.idxmax()]
print (au)
C bal date
0 7 1 2009-08-01
1 8 2 2009-08-01
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.