[英]Getting min and max Dates from a pandas dataframe
如何從數據框的主軸獲取最小和最大日期?
value
Date
2014-03-13 10000.000
2014-03-21 2000.000
2014-03-27 2000.000
2014-03-17 200.000
2014-03-17 5.000
2014-03-17 70.000
2014-03-21 200.000
2014-03-27 5.000
2014-03-27 25.000
2014-03-31 0.020
2014-03-31 12.000
2014-03-31 0.022
本質上,我想要一種獲取最小和最大日期的方法,即2014-03-13
和2014-03-31
。 我嘗試使用numpy.min
或df.min(axis=0)
,我能夠獲得最小值或最大值,但這不是我想要的
'日期'是你的索引,所以你想做,
print (df.index.min())
print (df.index.max())
2014-03-13 00:00:00
2014-03-31 00:00:00
min(df['some_property'])
max(df['some_property'])
內置函數適用於 Pandas Dataframes。
使用agg
確定一行中的最小值和最大值:
In [5]: df['Date'].agg(['min', 'max'])
Out[5]:
min 2014-03-13
max 2014-03-31
如果您想要的列在索引中,則必須先重置索引:
df.reset_index()['Date'].agg(['min', 'max'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.