【发布时间】:2015-10-30 06:04:58
【问题描述】:
我有一个看起来与此类似但有 10,000 行和 500 列的 Pandas DataFrame。
对于每一行,我想找出 3 天前 15:00 和今天 13:30 之间的最小值。
有没有一些原生的 numpy 方法可以快速做到这一点? 我的目标是能够通过说“从 3 天前 15:00 到 0 天前(又名今天)13:30 的最小值是多少?”来获得每一行的最小值?
对于这个特定示例,最后两行的答案是:
2011-01-09 2481.22
2011-01-10 2481.22
我现在的方式是这样的:
1. Get the earliest row (only the values after the start time)
2. Get the middle rows
3. Get the last row (only the values before the end time)
4. Concat (1), (2), and (3)
5. Get the minimum of (4)
但这在大型 DataFrame 上需要很长时间
下面的代码会生成一个类似的DF:
import numpy
import pandas
import datetime
numpy.random.seed(0)
random_numbers = (numpy.random.rand(10, 8)*100 + 2000)
columns = [datetime.time(13,0) , datetime.time(13,30), datetime.time(14,0), datetime.time(14,30) , datetime.time(15,0), datetime.time(15,30) ,datetime.time(16,0), datetime.time(16,30)]
index = pandas.date_range('2011/1/1', '2011/1/10')
df = pandas.DataFrame(data = random_numbers, columns=columns, index = index).astype(int)
print df
这是数据框的 json 版本:
'{"13:00:00":{"1293840000000":2085,"1293926400000":2062,"1294012800000":2035,"1294099200000":2086,"1294185600000":2006,"129409720":2000" “1294358400000”:2078,“1294444800000”:2055,“1294531200000”:2023,“1294617600000”:2024},“13:30:00”:{“1293840000000”:2045,“1293926400000”:2039,“1294012800000”: 2035,“1294099200000”:2025,“1294272000000”:2099,“1294358400000”:2028,“1294444800000”:2028,“1294531200000”:2034,“1294617600000”:2010},“14:00: 00“:{”1293840000000“:2095,”1293926400000“:2001,”1294099200000“:2032,”1294185600000“:2022,”1294272000000“:2040,”1294358400000“:2024,”1294444800000“: 2070,“1294531200000”:2095},“14:30:00”:2057,“1293926400”:2042,“1294012800000”:2018,“1294099200000”:2023,“1294185600000 ":2025,"1294272000000":2016,"1294358400000":2066,"1294444800000":2041,"1294531200000":2098,"1294617600000":2023},"15:00:00000":{"1293080," "1293926400000":2025,"1294012800000":2040,"1294099200000":2061,"129418 5600000":2013,"1294272000000":2063,"1294358400000":2024,"1294444800000":2036,"1294531200000":2096,"1294617600000":2068}""1294444800000":2096,"1294617600000":2068}"15:30:00000{00003:00003" ,“1293926400000”:2092,“1294099200000”:2001,“1294185600000”:2001,“1294272000000”:2049,“1294358400000”:2066,“1294444800000”:2082,“1294531200000”:2090,“ 1294617600000“:2005},”16:00:00“:2081,”1293926400000“:2003,”1294012800000“:2009,”1294099200000“:2001,”1294185600000“:2011,”1294272000000“:2098 ,“1294358400000”:2092,“1294531200000”:2029,“1294617600000”:2073},“16:30:00”:{“1293840000000”:2015,“1293926400000”:2095,“1294012800000” :2094,“1294099200000”:2061,“1294272000000”:2006,“1294358400000”:2042,“1294444800000”:2004,“1294531200000”:2099,“1294617600000”:2088}'' p >
【问题讨论】:
-
会先做一个
rolling_min来获得最后3 行每列的最小值,然后再做一个min来获得新行的最小值,得到你想要的结果吗? -
您是如何到达
2011-01-10 2481.22的?你能更详细地解释一下你的预期输出吗? -
当然。以 2011-01-10 的行为例。我想在 15:00 之后收集 3 天前(3 行前)的所有值(2011-01-07 15:30 值,2011-01-07 2011-01-07 16:00 值,2011-01 -07 16:30 值)直到今天(2011-01-10)13:30。所以基本上每个单元格都在 2011-01-07 15:30 到今天 13:30 之间。在我收集这些值之后,我得到了一堆的最小值。
-
在您之前删除的答案之一中,您说“时间范围的确切起点和终点会有所不同”。您能否也对此进行更多说明?
-
@chthonicdaemon 我已经添加了 python 代码来生成这个数据帧以及这个数据帧的 json 版本。列名是 datetime.time 类型,索引是原始版本中的 pandas.DatetimeIndex 类型。
标签: python arrays numpy pandas dataframe