python-过滤熊猫数据框以获取最大日期和符号

前端之家收集整理的这篇文章主要介绍了python-过滤熊猫数据框以获取最大日期和符号 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在这里问了一个问题,但没有错误,但是有一个空数据框和列.我究竟做错了什么 ?

我的原始数据框如下

               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

以下语句可以工作,但给我一个空的数据框:

df.loc[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())] 

链接
unsupported operand type(s) for &: ‘str’ and ‘Timestamp’

最佳答案
@H_403_24@通过to_datetime将列转换为日期时间,也不需要loc,因此应将其删除

df['Date'] = pd.to_datetime(df['Date'])
df[(df['Symbol'] == "TLT") & (df['Date'] == df['Date'].max())] 
原文链接:https://www.f2er.com/python/533158.html

猜你在找的Python相关文章