【发布时间】:2019-04-22 17:48:34
【问题描述】:
我正在尝试过滤具有不同日期的不同水果的数据框中的行,我只想获取每个水果的最新日期的行。
我在 python 3 中这样做。
import pandas as pd
d = {'Fruit':[Melon, Melon, Melon, Apple,Apple],
'Date':[203313, 414214, 511515,123223,501010]}
df = pd.DataFrame(d)
print(df)
输出:
Date Fruit
0 203313 Melon
1 414214 Melon
2 511515 Melon
3 123223 Apple
4 501010 Apple
在上面的例子df中,正确的结果是Melon, 511515和Apple 501010。
【问题讨论】:
-
df.loc[df.Date.max().index] -
AttributeError: 'Timestamp' 对象没有属性 'index'
-
df.loc[df.Date.eq(df.Date.max())]
标签: python python-3.x pandas dataframe pandas-groupby