如何根据日期时间列从数据框中提取子集?

如何根据日期时间列从数据框中提取子集?

问题描述:

我有一个具有以下结构的数据框.

I have a dataframe with the following structure.

我想根据datetime列在文本列中以两周为一组选择数据.

I would like to select the data in the text column in chunks of two weeks, based upon the datetime column.

最有效的方法是什么?

     text_column        datetime_column
0      jfklsjf        2014-05-10 22:42:35
1      ldjskfj        2014-05-14 03:04:24
2      dslffkf        2014-07-03 23:05:17
.         .                     .
.         .                     .

如果您要查找特定的块,则可以使用熊猫进行常规过滤

If you are looking for specific chunks, you can do just a normal filter using pandas

df[(df['datetime_colum']>='2014-04-10') & (df['datetime_column']<'2014-05-10')]

在我的示例中,您只会得到2014年10月4日的日期.您可以将日期更改为更多的日期,如果尚未这样做,请确保datetime列是pandas datetime列.您可以通过

My example there will get you just the days that are for october 4th 2014. You can change the dates to get more days, if you don't already before doing this make sure the datetime column is an pandas datetime column. You can do this by

df['datetime_column']= pd.to_datetime(df['datetime_column'])