python-2.7 - Pandas:将 'crop' 作为大型数据帧仅存储到前 1000 天的最佳方法是什么?

标签 python-2.7 pandas dataframe

我有一个数据框,其中索引由日期时间组成。我还有一个锚定日期,并且我知道我只希望第二个数据帧包含锚定日期之前的 1000 天。最好的方法是什么?

最佳答案

不知道这是否是最好的方法,但应该有效

创建示例数据帧:

>>> dates = [pd.datetime(2012, 5, 4), pd.datetime(2012, 5, 5), pd.datetime(2012, 5, 6), pd.datetime(2012, 5, 1), pd.datetime(2012, 5, 2), pd.datetime(2012, 5, 3)]
>>> values = [1, 2, 3, 4, 5, 6]
>>> df = pd.DataFrame(values, dates)
>>> df
>>> df
            0
2012-05-04  1
2012-05-05  2
2012-05-06  3
2012-05-01  4
2012-05-02  5
2012-05-03  6

假设我们想要从 2012 年 5 月 4 日起回溯 2 天:

>>> date_end = pd.datetime(2012, 5, 4)
>>> date_start = date_end - pd.DateOffset(days=2)
>>> date_start, date_end
(datetime.datetime(2012, 5, 2, 0, 0), datetime.datetime(2012, 5, 4, 0, 0))

现在让我们尝试按 label indexing 获取行:

>>> df.loc[date_start:date_end]
Empty DataFrame
Columns: [0]
Index: []

这是因为我们的索引未排序,所以让我们修复它:

>>> df.sort_index(inplace=True)
>>> df.loc[date_start:date_end]
            0
2012-05-02  5
2012-05-03  6
2012-05-04  1

也可以通过 datetime indexing 获取行:

>>> df[date_start:date_end]
            0
2012-05-02  5
2012-05-03  6
2012-05-04  1

请记住,我仍然不是 Pandas 方面的专家,但我非常喜欢它进行数据分析。

希望有帮助。

关于python-2.7 - Pandas:将 'crop' 作为大型数据帧仅存储到前 1000 天的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19602406/

相关文章:

python - 如何将多个 .xls 文件与 python 中的超链接合并?

python - 根据其他列的值作为一组设置列中的值

python - "-inf "在python中是什么意思?

python - Scrapy导入错误: cannot import name "______Item"

python - 如何查找用户在 Django 应用程序中的登录总数?

python - 在 Python Pandas 中查找 ID 的最小值、最大值和平均值

python - 调用函数时出现问题

python - 在Python中比较2个数据帧中的行时的if语句

python - 为什么 list(xrange) 比 range() 慢?

PYTHONPATH 与符号链接(symbolic link)