I asked a question here and I don't get an error but an empty dataframe with columns. What am I doing wrong ?
My original data frame is below
Date Symbol
49 2018-11-27 0
50 2018-12-10 0
51 2018-12-17 0
52 2018-12-27 XLK
53 2018-12-27 XLV
54 2018-12-28 VTV
55 2019-01-09 0
56 2019-01-09 0
57 2019-01-16 0
58 2019-02-04 0
59 2019-02-04 0
61 2019-02-05 SPY
62 2019-02-05 0
60 2019-02-05 TLT
63 2019-02-07 TLT
64 2019-02-09 0
The statement following statement works but gives me an empty dataframe :
df.loc[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())]
link unsupported operand type(s) for &: 'str' and 'Timestamp'
Convert column to datetimes by to_datetime
, also loc
is not necessare, so should be removed:
df['Date'] = pd.to_datetime(df['Date'])
df[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())]
Just to add some ideas. You can also use idxmax()
or idxmin()
df.loc[df.groupby('Date')['Symbol'].idxmax()]
This will give you all maximum Date
of each Symbol
categories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With