将数据帧拆分为多个数据帧

时间:2018-04-21 11:53:03

标签: python pandas dataframe time-series

我有以下代码:

Date                   X          
...
2014-12-30 23:00:00    2
2014-12-30 23:15:00    0
2014-12-30 23:30:00    1
2014-12-30 23:45:00    1
2014-12-31 00:00:00    22
...
2015-01-01 00:00:00    0
2015-01-02 00:00:00    2
2015-01-03 00:00:00    2
2015-01-04 00:00:00    2
2015-01-04 00:00:00    2
2015-01-05 00:00:00    2
...

我想将此时间序列(数据帧)拆分为多个时间序列(数据帧)。我希望每个星期一有一个时间序列,一个星期二,星期三......等等。

我怎么能用熊猫做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以使用dictionary of DataFramesweekday_name创建groupby

dfs = dict(tuple(df.groupby(df['Date'].dt.weekday_name)))

#select by days
print (dfs['Friday'])
        Date  X
6 2015-01-02  2

print (dfs['Thursday'])

        Date  X
5 2015-01-01  0

<强>详细

print (df['Date'].dt.weekday_name)
0       Tuesday
1       Tuesday
2       Tuesday
3       Tuesday
4     Wednesday
5      Thursday
6        Friday
7      Saturday
8        Sunday
9        Sunday
10       Monday
Name: Date, dtype: object