根据日期将数据框分为两部分 [英] Split dataframe into two on the basis of date

查看:59
本文介绍了根据日期将数据框分为两部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1000个这样的数据集

I have dataset with 1000 rows like this

 Date,      Cost,         Quantity(in ton),    Source,          Unloading Station
    01/10/2015, 7,            5.416,               XYZ,           ABC

我想拆分以日期为基础的数据。例如直到2016年12月20日为止都是培训数据,之后是测试数据。

i want to split the data on the base of date. For e.g. till date 20.12.2016 is a training data and after that it is test data.

我应该如何拆分?

推荐答案

您可以通过将列转换为pandas to_datetime类型并将其设置为索引来轻松地做到这一点。

You can easily do that by converting your column to pandas to_datetime type and set it as index.

import pandas as pd
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index(df['Date'])
df = df.sort_index()

一旦使用这种格式的数据,就可以简单地将日期用作创建分区的索引,如下所示:

Once you have your data in this format, you can simply use date as index for creating partition as follows:

# create train test partition
train = df['2015-01-10':'2016-12-20']
test  = df['2016-12-21':]
print('Train Dataset:',train.shape)
print('Test Dataset:',test.shape)

这篇关于根据日期将数据框分为两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆